<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Model field reference &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="Models" href="index.html" />
    <link rel="next" title="Related objects reference" href="relations.html" />
    <link rel="prev" title="Models" href="index.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="Models">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="relations.html" title="Related objects reference">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-models-fields">
            
  <div class="section" id="s-module-django.db.models.fields">
<span id="s-model-field-reference"></span><span id="module-django.db.models.fields"></span><span id="model-field-reference"></span><h1>Model field reference<a class="headerlink" href="#module-django.db.models.fields" title="Permalink to this headline">¶</a></h1>
<p>This document contains all the API references of <a class="reference internal" href="#django.db.models.Field" title="django.db.models.Field"><tt class="xref py py-class docutils literal"><span class="pre">Field</span></tt></a> including the
<a class="reference internal" href="#field-options">field options</a> and <a class="reference internal" href="#field-types">field types</a> Django offers.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">If the built-in fields don&#8217;t do the trick, you can try <a class="reference internal" href="../../topics/localflavor.html"><em>localflavor</em></a>, which contains assorted pieces of code
that are useful for particular countries or cultures. Also, you can easily
<a class="reference internal" href="../../howto/custom-model-fields.html"><em>write your own custom model fields</em></a>.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Technically, these models are defined in <a class="reference internal" href="#module-django.db.models.fields" title="django.db.models.fields: Built-in field types."><tt class="xref py py-mod docutils literal"><span class="pre">django.db.models.fields</span></tt></a>, but
for convenience they&#8217;re imported into <a class="reference internal" href="../../topics/db/models.html#module-django.db.models" title="django.db.models"><tt class="xref py py-mod docutils literal"><span class="pre">django.db.models</span></tt></a>; the standard
convention is to use <tt class="docutils literal"><span class="pre">from</span> <span class="pre">django.db</span> <span class="pre">import</span> <span class="pre">models</span></tt> and refer to fields as
<tt class="docutils literal"><span class="pre">models.&lt;Foo&gt;Field</span></tt>.</p>
</div>
<div class="section" id="s-field-options">
<span id="s-common-model-field-options"></span><span id="field-options"></span><span id="common-model-field-options"></span><h2>Field options<a class="headerlink" href="#field-options" title="Permalink to this headline">¶</a></h2>
<p>The following arguments are available to all field types. All are optional.</p>
<div class="section" id="s-null">
<span id="null"></span><h3><tt class="docutils literal"><span class="pre">null</span></tt><a class="headerlink" href="#null" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.null">
<tt class="descclassname">Field.</tt><tt class="descname">null</tt><a class="headerlink" href="#django.db.models.Field.null" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <tt class="docutils literal"><span class="pre">True</span></tt>, Django will store empty values as <tt class="docutils literal"><span class="pre">NULL</span></tt> in the database. Default
is <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<p>Avoid using <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a> on string-based fields such as
<a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> and <a class="reference internal" href="#django.db.models.TextField" title="django.db.models.TextField"><tt class="xref py py-class docutils literal"><span class="pre">TextField</span></tt></a> because empty string values will
always be stored as empty strings, not as <tt class="docutils literal"><span class="pre">NULL</span></tt>. If a string-based field has
<tt class="docutils literal"><span class="pre">null=True</span></tt>, that means it has two possible values for &#8220;no data&#8221;: <tt class="docutils literal"><span class="pre">NULL</span></tt>,
and the empty string. In most cases, it&#8217;s redundant to have two possible values
for &#8220;no data;&#8221; the Django convention is to use the empty string, not <tt class="docutils literal"><span class="pre">NULL</span></tt>.</p>
<p>For both string-based and non-string-based fields, you will also need to
set <tt class="docutils literal"><span class="pre">blank=True</span></tt> if you wish to permit empty values in forms, as the
<a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a> parameter only affects database storage
(see <a class="reference internal" href="#django.db.models.Field.blank" title="django.db.models.Field.blank"><tt class="xref py py-attr docutils literal"><span class="pre">blank</span></tt></a>).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When using the Oracle database backend, the value <tt class="docutils literal"><span class="pre">NULL</span></tt> will be stored to
denote the empty string regardless of this attribute.</p>
</div>
<p>If you want to accept <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a> values with <a class="reference internal" href="#django.db.models.BooleanField" title="django.db.models.BooleanField"><tt class="xref py py-class docutils literal"><span class="pre">BooleanField</span></tt></a>,
use <a class="reference internal" href="#django.db.models.NullBooleanField" title="django.db.models.NullBooleanField"><tt class="xref py py-class docutils literal"><span class="pre">NullBooleanField</span></tt></a> instead.</p>
</div>
<div class="section" id="s-blank">
<span id="blank"></span><h3><tt class="docutils literal"><span class="pre">blank</span></tt><a class="headerlink" href="#blank" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.blank">
<tt class="descclassname">Field.</tt><tt class="descname">blank</tt><a class="headerlink" href="#django.db.models.Field.blank" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <tt class="docutils literal"><span class="pre">True</span></tt>, the field is allowed to be blank. Default is <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<p>Note that this is different than <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a>. <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a> is
purely database-related, whereas <a class="reference internal" href="#django.db.models.Field.blank" title="django.db.models.Field.blank"><tt class="xref py py-attr docutils literal"><span class="pre">blank</span></tt></a> is validation-related. If
a field has <tt class="docutils literal"><span class="pre">blank=True</span></tt>, form validation will allow entry of an empty value.
If a field has <tt class="docutils literal"><span class="pre">blank=False</span></tt>, the field will be required.</p>
</div>
<div class="section" id="s-choices">
<span id="s-field-choices"></span><span id="choices"></span><span id="field-choices"></span><h3><tt class="docutils literal"><span class="pre">choices</span></tt><a class="headerlink" href="#choices" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.choices">
<tt class="descclassname">Field.</tt><tt class="descname">choices</tt><a class="headerlink" href="#django.db.models.Field.choices" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An iterable (e.g., a list or tuple) consisting itself of iterables of exactly
two items (e.g. <tt class="docutils literal"><span class="pre">[(A,</span> <span class="pre">B),</span> <span class="pre">(A,</span> <span class="pre">B)</span> <span class="pre">...]</span></tt>) to use as choices for this field. If
this is given, the default form widget will be a select box with these choices
instead of the standard text field.</p>
<p>The first element in each tuple is the actual value to be set on the model,
and the second element is the human-readable name. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">YEAR_IN_SCHOOL_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="p">(</span><span class="s">&#39;FR&#39;</span><span class="p">,</span> <span class="s">&#39;Freshman&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;SO&#39;</span><span class="p">,</span> <span class="s">&#39;Sophomore&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;JR&#39;</span><span class="p">,</span> <span class="s">&#39;Junior&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;SR&#39;</span><span class="p">,</span> <span class="s">&#39;Senior&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Generally, it&#8217;s best to define choices inside a model class, and to
define a suitably-named constant for each value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Student</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">FRESHMAN</span> <span class="o">=</span> <span class="s">&#39;FR&#39;</span>
    <span class="n">SOPHOMORE</span> <span class="o">=</span> <span class="s">&#39;SO&#39;</span>
    <span class="n">JUNIOR</span> <span class="o">=</span> <span class="s">&#39;JR&#39;</span>
    <span class="n">SENIOR</span> <span class="o">=</span> <span class="s">&#39;SR&#39;</span>
    <span class="n">YEAR_IN_SCHOOL_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="n">FRESHMAN</span><span class="p">,</span> <span class="s">&#39;Freshman&#39;</span><span class="p">),</span>
        <span class="p">(</span><span class="n">SOPHOMORE</span><span class="p">,</span> <span class="s">&#39;Sophomore&#39;</span><span class="p">),</span>
        <span class="p">(</span><span class="n">JUNIOR</span><span class="p">,</span> <span class="s">&#39;Junior&#39;</span><span class="p">),</span>
        <span class="p">(</span><span class="n">SENIOR</span><span class="p">,</span> <span class="s">&#39;Senior&#39;</span><span class="p">),</span>
    <span class="p">)</span>
    <span class="n">year_in_school</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
                                      <span class="n">choices</span><span class="o">=</span><span class="n">YEAR_IN_SCHOOL_CHOICES</span><span class="p">,</span>
                                      <span class="n">default</span><span class="o">=</span><span class="n">FRESHMAN</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_upperclass</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">year_in_school</span> <span class="ow">in</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">JUNIOR</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">SENIOR</span><span class="p">)</span>
</pre></div>
</div>
<p>Though you can define a choices list outside of a model class and then
refer to it, defining the choices and names for each choice inside the
model class keeps all of that information with the class that uses it,
and makes the choices easy to reference (e.g, <tt class="docutils literal"><span class="pre">Student.SOPHOMORE</span></tt>
will work anywhere that the <tt class="docutils literal"><span class="pre">Student</span></tt> model has been imported).</p>
<p>You can also collect your available choices into named groups that can
be used for organizational purposes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">MEDIA_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="p">(</span><span class="s">&#39;Audio&#39;</span><span class="p">,</span> <span class="p">(</span>
            <span class="p">(</span><span class="s">&#39;vinyl&#39;</span><span class="p">,</span> <span class="s">&#39;Vinyl&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="s">&#39;cd&#39;</span><span class="p">,</span> <span class="s">&#39;CD&#39;</span><span class="p">),</span>
        <span class="p">)</span>
    <span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;Video&#39;</span><span class="p">,</span> <span class="p">(</span>
            <span class="p">(</span><span class="s">&#39;vhs&#39;</span><span class="p">,</span> <span class="s">&#39;VHS Tape&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="s">&#39;dvd&#39;</span><span class="p">,</span> <span class="s">&#39;DVD&#39;</span><span class="p">),</span>
        <span class="p">)</span>
    <span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;unknown&#39;</span><span class="p">,</span> <span class="s">&#39;Unknown&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The first element in each tuple is the name to apply to the group. The
second element is an iterable of 2-tuples, with each 2-tuple containing
a value and a human-readable name for an option. Grouped options may be
combined with ungrouped options within a single list (such as the
<cite>unknown</cite> option in this example).</p>
<p>For each model field that has <a class="reference internal" href="#django.db.models.Field.choices" title="django.db.models.Field.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> set, Django will add a
method to retrieve the human-readable name for the field&#8217;s current value. See
<a class="reference internal" href="instances.html#django.db.models.Model.get_FOO_display" title="django.db.models.Model.get_FOO_display"><tt class="xref py py-meth docutils literal"><span class="pre">get_FOO_display()</span></tt></a> in the database API
documentation.</p>
<p>Note that choices can be any iterable object &#8211; not necessarily a list or tuple.
This lets you construct choices dynamically. But if you find yourself hacking
<a class="reference internal" href="#django.db.models.Field.choices" title="django.db.models.Field.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> to be dynamic, you&#8217;re probably better off using a proper
database table with a <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>. <a class="reference internal" href="#django.db.models.Field.choices" title="django.db.models.Field.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> is meant for
static data that doesn&#8217;t change much, if ever.</p>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Unless <a class="reference internal" href="#django.db.models.Field.blank" title="django.db.models.Field.blank"><tt class="xref py py-attr docutils literal"><span class="pre">blank=False</span></tt></a> is set on the field along with a
<a class="reference internal" href="#django.db.models.Field.default" title="django.db.models.Field.default"><tt class="xref py py-attr docutils literal"><span class="pre">default</span></tt></a> then a label containing <tt class="docutils literal"><span class="pre">&quot;---------&quot;</span></tt> will be rendered
with the select box. To override this behavior, add a tuple to <tt class="docutils literal"><span class="pre">choices</span></tt>
containing <tt class="docutils literal"><span class="pre">None</span></tt>; e.g. <tt class="docutils literal"><span class="pre">(None,</span> <span class="pre">'Your</span> <span class="pre">String</span> <span class="pre">For</span> <span class="pre">Display')</span></tt>.
Alternatively, you can use an empty string instead of <tt class="docutils literal"><span class="pre">None</span></tt> where this makes
sense - such as on a <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a>.</p>
</div>
<div class="section" id="s-db-column">
<span id="db-column"></span><h3><tt class="docutils literal"><span class="pre">db_column</span></tt><a class="headerlink" href="#db-column" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.db_column">
<tt class="descclassname">Field.</tt><tt class="descname">db_column</tt><a class="headerlink" href="#django.db.models.Field.db_column" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The name of the database column to use for this field. If this isn&#8217;t given,
Django will use the field&#8217;s name.</p>
<p>If your database column name is an SQL reserved word, or contains
characters that aren&#8217;t allowed in Python variable names &#8211; notably, the
hyphen &#8211; that&#8217;s OK. Django quotes column and table names behind the
scenes.</p>
</div>
<div class="section" id="s-db-index">
<span id="db-index"></span><h3><tt class="docutils literal"><span class="pre">db_index</span></tt><a class="headerlink" href="#db-index" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.db_index">
<tt class="descclassname">Field.</tt><tt class="descname">db_index</tt><a class="headerlink" href="#django.db.models.Field.db_index" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <tt class="docutils literal"><span class="pre">True</span></tt>, <a class="reference internal" href="../django-admin.html#django-admin-sqlindexes"><tt class="xref std std-djadmin docutils literal"><span class="pre">django-admin.py</span> <span class="pre">sqlindexes</span></tt></a> will output a
<tt class="docutils literal"><span class="pre">CREATE</span> <span class="pre">INDEX</span></tt> statement for this field.</p>
</div>
<div class="section" id="s-db-tablespace">
<span id="db-tablespace"></span><h3><tt class="docutils literal"><span class="pre">db_tablespace</span></tt><a class="headerlink" href="#db-tablespace" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.db_tablespace">
<tt class="descclassname">Field.</tt><tt class="descname">db_tablespace</tt><a class="headerlink" href="#django.db.models.Field.db_tablespace" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The name of the <a class="reference internal" href="../../topics/db/tablespaces.html"><em>database tablespace</em></a> to use for
this field&#8217;s index, if this field is indexed. The default is the project&#8217;s
<a class="reference internal" href="../settings.html#std:setting-DEFAULT_INDEX_TABLESPACE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_INDEX_TABLESPACE</span></tt></a> setting, if set, or the
<a class="reference internal" href="options.html#django.db.models.Options.db_tablespace" title="django.db.models.Options.db_tablespace"><tt class="xref py py-attr docutils literal"><span class="pre">db_tablespace</span></tt></a> of the model, if any. If the backend doesn&#8217;t
support tablespaces for indexes, this option is ignored.</p>
</div>
<div class="section" id="s-default">
<span id="default"></span><h3><tt class="docutils literal"><span class="pre">default</span></tt><a class="headerlink" href="#default" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.default">
<tt class="descclassname">Field.</tt><tt class="descname">default</tt><a class="headerlink" href="#django.db.models.Field.default" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The default value for the field. This can be a value or a callable object. If
callable it will be called every time a new object is created.</p>
<p>The default cannot be a mutable object (model instance, list, set, etc.), as a
reference to the same instance of that object would be used as the default
value in all new model instances. Instead, wrap the desired default in a
callable.  For example, if you had a custom <tt class="docutils literal"><span class="pre">JSONField</span></tt> and wanted to specify
a dictionary as the default, use a function as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">contact_default</span><span class="p">():</span>
    <span class="k">return</span> <span class="p">{</span><span class="s">&quot;email&quot;</span><span class="p">:</span> <span class="s">&quot;to1@example.com&quot;</span><span class="p">}</span>

<span class="n">contact_info</span> <span class="o">=</span> <span class="n">JSONField</span><span class="p">(</span><span class="s">&quot;ContactInfo&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">contact_default</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that <tt class="docutils literal"><span class="pre">lambda</span></tt>s cannot be used for field options like <tt class="docutils literal"><span class="pre">default</span></tt>
because they cannot be <a class="reference internal" href="../../topics/migrations.html#migration-serializing"><em>serialized by migrations</em></a>.
See that documentation for other caveats.</p>
</div>
<div class="section" id="s-editable">
<span id="editable"></span><h3><tt class="docutils literal"><span class="pre">editable</span></tt><a class="headerlink" href="#editable" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.editable">
<tt class="descclassname">Field.</tt><tt class="descname">editable</tt><a class="headerlink" href="#django.db.models.Field.editable" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <tt class="docutils literal"><span class="pre">False</span></tt>, the field will not be displayed in the admin or any other
<a class="reference internal" href="../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><tt class="xref py py-class docutils literal"><span class="pre">ModelForm</span></tt></a>. They are also skipped during <a class="reference internal" href="instances.html#validating-objects"><em>model
validation</em></a>. Default is <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</div>
<div class="section" id="s-error-messages">
<span id="error-messages"></span><h3><tt class="docutils literal"><span class="pre">error_messages</span></tt><a class="headerlink" href="#error-messages" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.error_messages">
<tt class="descclassname">Field.</tt><tt class="descname">error_messages</tt><a class="headerlink" href="#django.db.models.Field.error_messages" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <tt class="docutils literal"><span class="pre">error_messages</span></tt> argument lets you override the default messages that the
field will raise. Pass in a dictionary with keys matching the error messages you
want to override.</p>
<p>Error message keys include <tt class="docutils literal"><span class="pre">null</span></tt>, <tt class="docutils literal"><span class="pre">blank</span></tt>, <tt class="docutils literal"><span class="pre">invalid</span></tt>, <tt class="docutils literal"><span class="pre">invalid_choice</span></tt>,
<tt class="docutils literal"><span class="pre">unique</span></tt>, and <tt class="docutils literal"><span class="pre">unique_for_date</span></tt>. Additional error message keys are
specified for each field in the <a class="reference internal" href="#field-types">Field types</a> section below.</p>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>The <tt class="docutils literal"><span class="pre">unique_for_date</span></tt> error message key was added.</p>
</div>
<div class="section" id="s-help-text">
<span id="help-text"></span><h3><tt class="docutils literal"><span class="pre">help_text</span></tt><a class="headerlink" href="#help-text" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.help_text">
<tt class="descclassname">Field.</tt><tt class="descname">help_text</tt><a class="headerlink" href="#django.db.models.Field.help_text" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Extra &#8220;help&#8221; text to be displayed with the form widget. It&#8217;s useful for
documentation even if your field isn&#8217;t used on a form.</p>
<p>Note that this value is <em>not</em> HTML-escaped in automatically-generated
forms. This lets you include HTML in <a class="reference internal" href="#django.db.models.Field.help_text" title="django.db.models.Field.help_text"><tt class="xref py py-attr docutils literal"><span class="pre">help_text</span></tt></a> if you so
desire. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">help_text</span><span class="o">=</span><span class="s">&quot;Please use the following format: &lt;em&gt;YYYY-MM-DD&lt;/em&gt;.&quot;</span>
</pre></div>
</div>
<p>Alternatively you can use plain text and
<tt class="docutils literal"><span class="pre">django.utils.html.escape()</span></tt> to escape any HTML special characters. Ensure
that you escape any help text that may come from untrusted users to avoid a
cross-site scripting attack.</p>
</div>
<div class="section" id="s-primary-key">
<span id="primary-key"></span><h3><tt class="docutils literal"><span class="pre">primary_key</span></tt><a class="headerlink" href="#primary-key" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.primary_key">
<tt class="descclassname">Field.</tt><tt class="descname">primary_key</tt><a class="headerlink" href="#django.db.models.Field.primary_key" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <tt class="docutils literal"><span class="pre">True</span></tt>, this field is the primary key for the model.</p>
<p>If you don&#8217;t specify <tt class="docutils literal"><span class="pre">primary_key=True</span></tt> for any field in your model, Django
will automatically add an <a class="reference internal" href="#django.db.models.AutoField" title="django.db.models.AutoField"><tt class="xref py py-class docutils literal"><span class="pre">AutoField</span></tt></a> to hold the primary key, so you
don&#8217;t need to set <tt class="docutils literal"><span class="pre">primary_key=True</span></tt> on any of your fields unless you want to
override the default primary-key behavior. For more, see
<a class="reference internal" href="../../topics/db/models.html#automatic-primary-key-fields"><em>Automatic primary key fields</em></a>.</p>
<p><tt class="docutils literal"><span class="pre">primary_key=True</span></tt> implies <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null=False</span></tt></a> and
<a class="reference internal" href="#django.db.models.Field.unique" title="django.db.models.Field.unique"><tt class="xref py py-attr docutils literal"><span class="pre">unique=True</span></tt></a>. Only one primary key is allowed on an
object.</p>
<p>The primary key field is read-only. If you change the value of the primary
key on an existing object and then save it, a new object will be created
alongside the old one.</p>
</div>
<div class="section" id="s-unique">
<span id="unique"></span><h3><tt class="docutils literal"><span class="pre">unique</span></tt><a class="headerlink" href="#unique" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.unique">
<tt class="descclassname">Field.</tt><tt class="descname">unique</tt><a class="headerlink" href="#django.db.models.Field.unique" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <tt class="docutils literal"><span class="pre">True</span></tt>, this field must be unique throughout the table.</p>
<p>This is enforced at the database level and by model validation. If
you try to save a model with a duplicate value in a <a class="reference internal" href="#django.db.models.Field.unique" title="django.db.models.Field.unique"><tt class="xref py py-attr docutils literal"><span class="pre">unique</span></tt></a>
field, a <a class="reference internal" href="../exceptions.html#django.db.IntegrityError" title="django.db.IntegrityError"><tt class="xref py py-exc docutils literal"><span class="pre">django.db.IntegrityError</span></tt></a> will be raised by the model&#8217;s
<a class="reference internal" href="instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><tt class="xref py py-meth docutils literal"><span class="pre">save()</span></tt></a> method.</p>
<p>This option is valid on all field types except <a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a>,
<a class="reference internal" href="#django.db.models.OneToOneField" title="django.db.models.OneToOneField"><tt class="xref py py-class docutils literal"><span class="pre">OneToOneField</span></tt></a>, and <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a>.</p>
<p>Note that when <tt class="docutils literal"><span class="pre">unique</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, you don&#8217;t need to specify
<a class="reference internal" href="#django.db.models.Field.db_index" title="django.db.models.Field.db_index"><tt class="xref py py-attr docutils literal"><span class="pre">db_index</span></tt></a>, because <tt class="docutils literal"><span class="pre">unique</span></tt> implies the creation of an index.</p>
</div>
<div class="section" id="s-unique-for-date">
<span id="unique-for-date"></span><h3><tt class="docutils literal"><span class="pre">unique_for_date</span></tt><a class="headerlink" href="#unique-for-date" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.unique_for_date">
<tt class="descclassname">Field.</tt><tt class="descname">unique_for_date</tt><a class="headerlink" href="#django.db.models.Field.unique_for_date" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Set this to the name of a <a class="reference internal" href="#django.db.models.DateField" title="django.db.models.DateField"><tt class="xref py py-class docutils literal"><span class="pre">DateField</span></tt></a> or <a class="reference internal" href="#django.db.models.DateTimeField" title="django.db.models.DateTimeField"><tt class="xref py py-class docutils literal"><span class="pre">DateTimeField</span></tt></a> to
require that this field be unique for the value of the date field.</p>
<p>For example, if you have a field <tt class="docutils literal"><span class="pre">title</span></tt> that has
<tt class="docutils literal"><span class="pre">unique_for_date=&quot;pub_date&quot;</span></tt>, then Django wouldn&#8217;t allow the entry of two
records with the same <tt class="docutils literal"><span class="pre">title</span></tt> and <tt class="docutils literal"><span class="pre">pub_date</span></tt>.</p>
<p>Note that if you set this to point to a <a class="reference internal" href="#django.db.models.DateTimeField" title="django.db.models.DateTimeField"><tt class="xref py py-class docutils literal"><span class="pre">DateTimeField</span></tt></a>, only the date
portion of the field will be considered. Besides, when <a class="reference internal" href="../settings.html#std:setting-USE_TZ"><tt class="xref std std-setting docutils literal"><span class="pre">USE_TZ</span></tt></a> is
<tt class="docutils literal"><span class="pre">True</span></tt>, the check will be performed in the <a class="reference internal" href="../../topics/i18n/timezones.html#default-current-time-zone"><em>current time zone</em></a> at the time the object gets saved.</p>
<p>This is enforced by <a class="reference internal" href="instances.html#django.db.models.Model.validate_unique" title="django.db.models.Model.validate_unique"><tt class="xref py py-meth docutils literal"><span class="pre">Model.validate_unique()</span></tt></a> during model validation
but not at the database level. If any <a class="reference internal" href="#django.db.models.Field.unique_for_date" title="django.db.models.Field.unique_for_date"><tt class="xref py py-attr docutils literal"><span class="pre">unique_for_date</span></tt></a> constraint
involves fields that are not part of a <a class="reference internal" href="../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><tt class="xref py py-class docutils literal"><span class="pre">ModelForm</span></tt></a> (for
example, if one of the fields is listed in <tt class="docutils literal"><span class="pre">exclude</span></tt> or has
<a class="reference internal" href="#django.db.models.Field.editable" title="django.db.models.Field.editable"><tt class="xref py py-attr docutils literal"><span class="pre">editable=False</span></tt></a>), <a class="reference internal" href="instances.html#django.db.models.Model.validate_unique" title="django.db.models.Model.validate_unique"><tt class="xref py py-meth docutils literal"><span class="pre">Model.validate_unique()</span></tt></a> will
skip validation for that particular constraint.</p>
</div>
<div class="section" id="s-unique-for-month">
<span id="unique-for-month"></span><h3><tt class="docutils literal"><span class="pre">unique_for_month</span></tt><a class="headerlink" href="#unique-for-month" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.unique_for_month">
<tt class="descclassname">Field.</tt><tt class="descname">unique_for_month</tt><a class="headerlink" href="#django.db.models.Field.unique_for_month" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Like <a class="reference internal" href="#django.db.models.Field.unique_for_date" title="django.db.models.Field.unique_for_date"><tt class="xref py py-attr docutils literal"><span class="pre">unique_for_date</span></tt></a>, but requires the field to be unique with
respect to the month.</p>
</div>
<div class="section" id="s-unique-for-year">
<span id="unique-for-year"></span><h3><tt class="docutils literal"><span class="pre">unique_for_year</span></tt><a class="headerlink" href="#unique-for-year" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.unique_for_year">
<tt class="descclassname">Field.</tt><tt class="descname">unique_for_year</tt><a class="headerlink" href="#django.db.models.Field.unique_for_year" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Like <a class="reference internal" href="#django.db.models.Field.unique_for_date" title="django.db.models.Field.unique_for_date"><tt class="xref py py-attr docutils literal"><span class="pre">unique_for_date</span></tt></a> and <a class="reference internal" href="#django.db.models.Field.unique_for_month" title="django.db.models.Field.unique_for_month"><tt class="xref py py-attr docutils literal"><span class="pre">unique_for_month</span></tt></a>.</p>
</div>
<div class="section" id="s-verbose-name">
<span id="verbose-name"></span><h3><tt class="docutils literal"><span class="pre">verbose_name</span></tt><a class="headerlink" href="#verbose-name" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.verbose_name">
<tt class="descclassname">Field.</tt><tt class="descname">verbose_name</tt><a class="headerlink" href="#django.db.models.Field.verbose_name" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A human-readable name for the field. If the verbose name isn&#8217;t given, Django
will automatically create it using the field&#8217;s attribute name, converting
underscores to spaces. See <a class="reference internal" href="../../topics/db/models.html#verbose-field-names"><em>Verbose field names</em></a>.</p>
</div>
<div class="section" id="s-validators">
<span id="validators"></span><h3><tt class="docutils literal"><span class="pre">validators</span></tt><a class="headerlink" href="#validators" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.validators">
<tt class="descclassname">Field.</tt><tt class="descname">validators</tt><a class="headerlink" href="#django.db.models.Field.validators" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A list of validators to run for this field. See the <a class="reference internal" href="../validators.html"><em>validators
documentation</em></a> for more information.</p>
<div class="section" id="s-registering-and-fetching-lookups">
<span id="registering-and-fetching-lookups"></span><h4>Registering and fetching lookups<a class="headerlink" href="#registering-and-fetching-lookups" title="Permalink to this headline">¶</a></h4>
<p><tt class="docutils literal"><span class="pre">Field</span></tt> implements the <a class="reference internal" href="lookups.html#lookup-registration-api"><em>lookup registration API</em></a>.
The API can be used to customize which lookups are available for a field class, and
how lookups are fetched from a field.</p>
</div>
</div>
</div>
<div class="section" id="s-field-types">
<span id="s-model-field-types"></span><span id="field-types"></span><span id="model-field-types"></span><h2>Field types<a class="headerlink" href="#field-types" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-autofield">
<span id="autofield"></span><h3><tt class="docutils literal"><span class="pre">AutoField</span></tt><a class="headerlink" href="#autofield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.AutoField">
<em class="property">class </em><tt class="descname">AutoField</tt>(<em>**options</em>)<a class="headerlink" href="#django.db.models.AutoField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An <a class="reference internal" href="#django.db.models.IntegerField" title="django.db.models.IntegerField"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a> that automatically increments
according to available IDs. You usually won&#8217;t need to use this directly; a
primary key field will automatically be added to your model if you don&#8217;t specify
otherwise. See <a class="reference internal" href="../../topics/db/models.html#automatic-primary-key-fields"><em>Automatic primary key fields</em></a>.</p>
</div>
<div class="section" id="s-bigintegerfield">
<span id="bigintegerfield"></span><h3><tt class="docutils literal"><span class="pre">BigIntegerField</span></tt><a class="headerlink" href="#bigintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.BigIntegerField">
<em class="property">class </em><tt class="descname">BigIntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.BigIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A 64 bit integer, much like an <a class="reference internal" href="#django.db.models.IntegerField" title="django.db.models.IntegerField"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a> except that it is
guaranteed to fit numbers from <tt class="docutils literal"><span class="pre">-9223372036854775808</span></tt> to
<tt class="docutils literal"><span class="pre">9223372036854775807</span></tt>. The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</p>
</div>
<div class="section" id="s-binaryfield">
<span id="binaryfield"></span><h3><tt class="docutils literal"><span class="pre">BinaryField</span></tt><a class="headerlink" href="#binaryfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.BinaryField">
<em class="property">class </em><tt class="descname">BinaryField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.BinaryField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="versionadded">
<span class="title">New in Django 1.6.</span> </div>
<p>A field to store raw binary data. It only supports <tt class="docutils literal"><span class="pre">bytes</span></tt> assignment. Be
aware that this field has limited functionality. For example, it is not possible
to filter a queryset on a <tt class="docutils literal"><span class="pre">BinaryField</span></tt> value.</p>
<div class="admonition-abusing-binaryfield admonition">
<p class="first admonition-title">Abusing <tt class="docutils literal"><span class="pre">BinaryField</span></tt></p>
<p class="last">Although you might think about storing files in the database, consider that
it is bad design in 99% of the cases. This field is <em>not</em> a replacement for
proper <a class="reference internal" href="../../howto/static-files/index.html"><em>static files</em></a> handling.</p>
</div>
</div>
<div class="section" id="s-booleanfield">
<span id="booleanfield"></span><h3><tt class="docutils literal"><span class="pre">BooleanField</span></tt><a class="headerlink" href="#booleanfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.BooleanField">
<em class="property">class </em><tt class="descname">BooleanField</tt>(<em>**options</em>)<a class="headerlink" href="#django.db.models.BooleanField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A true/false field.</p>
<p>The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.CheckboxInput" title="django.forms.CheckboxInput"><tt class="xref py py-class docutils literal"><span class="pre">CheckboxInput</span></tt></a>.</p>
<p>If you need to accept <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a> values then use
<a class="reference internal" href="#django.db.models.NullBooleanField" title="django.db.models.NullBooleanField"><tt class="xref py py-class docutils literal"><span class="pre">NullBooleanField</span></tt></a> instead.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>The default value of <tt class="docutils literal"><span class="pre">BooleanField</span></tt> was changed from <tt class="docutils literal"><span class="pre">False</span></tt> to
<tt class="docutils literal"><span class="pre">None</span></tt> when <a class="reference internal" href="#django.db.models.Field.default" title="django.db.models.Field.default"><tt class="xref py py-attr docutils literal"><span class="pre">Field.default</span></tt></a> isn&#8217;t defined.</p>
</div>
</div>
<div class="section" id="s-charfield">
<span id="charfield"></span><h3><tt class="docutils literal"><span class="pre">CharField</span></tt><a class="headerlink" href="#charfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.CharField">
<em class="property">class </em><tt class="descname">CharField</tt>(<em>max_length=None</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.CharField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A string field, for small- to large-sized strings.</p>
<p>For large amounts of text, use <a class="reference internal" href="#django.db.models.TextField" title="django.db.models.TextField"><tt class="xref py py-class docutils literal"><span class="pre">TextField</span></tt></a>.</p>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</p>
<p><a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> has one extra required argument:</p>
<dl class="attribute">
<dt id="django.db.models.CharField.max_length">
<tt class="descclassname">CharField.</tt><tt class="descname">max_length</tt><a class="headerlink" href="#django.db.models.CharField.max_length" title="Permalink to this definition">¶</a></dt>
<dd><p>The maximum length (in characters) of the field. The max_length is enforced
at the database level and in Django&#8217;s validation.</p>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you are writing an application that must be portable to multiple
database backends, you should be aware that there are restrictions on
<tt class="docutils literal"><span class="pre">max_length</span></tt> for some backends. Refer to the <a class="reference internal" href="../databases.html"><em>database backend
notes</em></a> for details.</p>
</div>
<div class="admonition-mysql-users admonition">
<p class="first admonition-title">MySQL users</p>
<p class="last">If you are using this field with MySQLdb 1.2.2 and the <tt class="docutils literal"><span class="pre">utf8_bin</span></tt>
collation (which is <em>not</em> the default), there are some issues to be aware
of. Refer to the <a class="reference internal" href="../databases.html#mysql-collation"><em>MySQL database notes</em></a> for
details.</p>
</div>
</div>
<div class="section" id="s-commaseparatedintegerfield">
<span id="commaseparatedintegerfield"></span><h3><tt class="docutils literal"><span class="pre">CommaSeparatedIntegerField</span></tt><a class="headerlink" href="#commaseparatedintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.CommaSeparatedIntegerField">
<em class="property">class </em><tt class="descname">CommaSeparatedIntegerField</tt>(<em>max_length=None</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.CommaSeparatedIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A field of integers separated by commas. As in <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a>, the
<a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> argument is required and the note about database
portability mentioned there should be heeded.</p>
</div>
<div class="section" id="s-datefield">
<span id="datefield"></span><h3><tt class="docutils literal"><span class="pre">DateField</span></tt><a class="headerlink" href="#datefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.DateField">
<em class="property">class </em><tt class="descname">DateField</tt>(<span class="optional">[</span><em>auto_now=False</em>, <em>auto_now_add=False</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.DateField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A date, represented in Python by a <tt class="docutils literal"><span class="pre">datetime.date</span></tt> instance. Has a few extra,
optional arguments:</p>
<dl class="attribute">
<dt id="django.db.models.DateField.auto_now">
<tt class="descclassname">DateField.</tt><tt class="descname">auto_now</tt><a class="headerlink" href="#django.db.models.DateField.auto_now" title="Permalink to this definition">¶</a></dt>
<dd><p>Automatically set the field to now every time the object is saved. Useful
for &#8220;last-modified&#8221; timestamps. Note that the current date is <em>always</em>
used; it&#8217;s not just a default value that you can override.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.DateField.auto_now_add">
<tt class="descclassname">DateField.</tt><tt class="descname">auto_now_add</tt><a class="headerlink" href="#django.db.models.DateField.auto_now_add" title="Permalink to this definition">¶</a></dt>
<dd><p>Automatically set the field to now when the object is first created. Useful
for creation of timestamps. Note that the current date is <em>always</em> used;
it&#8217;s not just a default value that you can override.</p>
</dd></dl>

<p>The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>. The admin adds a JavaScript calendar,
and a shortcut for &#8220;Today&#8221;. Includes an additional <tt class="docutils literal"><span class="pre">invalid_date</span></tt> error
message key.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">As currently implemented, setting <tt class="docutils literal"><span class="pre">auto_now</span></tt> or <tt class="docutils literal"><span class="pre">auto_now_add</span></tt> to
<tt class="docutils literal"><span class="pre">True</span></tt> will cause the field to have <tt class="docutils literal"><span class="pre">editable=False</span></tt> and <tt class="docutils literal"><span class="pre">blank=True</span></tt>
set.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <tt class="docutils literal"><span class="pre">auto_now</span></tt> and <tt class="docutils literal"><span class="pre">auto_now_add</span></tt> options will always use the date in
the <a class="reference internal" href="../../topics/i18n/timezones.html#default-current-time-zone"><em>default timezone</em></a> at the moment of
creation or update. If you need something different, you may want to
consider simply using your own callable default or overriding <tt class="docutils literal"><span class="pre">save()</span></tt>
instead of using <tt class="docutils literal"><span class="pre">auto_now</span></tt> or <tt class="docutils literal"><span class="pre">auto_now_add</span></tt>; or using a
<tt class="docutils literal"><span class="pre">DateTimeField</span></tt> instead of a <tt class="docutils literal"><span class="pre">DateField</span></tt> and deciding how to handle the
conversion from datetime to date at display time.</p>
</div>
</div>
<div class="section" id="s-datetimefield">
<span id="datetimefield"></span><h3><tt class="docutils literal"><span class="pre">DateTimeField</span></tt><a class="headerlink" href="#datetimefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.DateTimeField">
<em class="property">class </em><tt class="descname">DateTimeField</tt>(<span class="optional">[</span><em>auto_now=False</em>, <em>auto_now_add=False</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.DateTimeField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A date and time, represented in Python by a <tt class="docutils literal"><span class="pre">datetime.datetime</span></tt> instance.
Takes the same extra arguments as <a class="reference internal" href="#django.db.models.DateField" title="django.db.models.DateField"><tt class="xref py py-class docutils literal"><span class="pre">DateField</span></tt></a>.</p>
<p>The default form widget for this field is a single
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>. The admin uses two separate
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a> widgets with JavaScript shortcuts.</p>
</div>
<div class="section" id="s-decimalfield">
<span id="decimalfield"></span><h3><tt class="docutils literal"><span class="pre">DecimalField</span></tt><a class="headerlink" href="#decimalfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.DecimalField">
<em class="property">class </em><tt class="descname">DecimalField</tt>(<em>max_digits=None</em>, <em>decimal_places=None</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.DecimalField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A fixed-precision decimal number, represented in Python by a
<a class="reference external" href="http://docs.python.org/3/library/decimal.html#decimal.Decimal" title="(in Python v3.4)"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance. Has two <strong>required</strong> arguments:</p>
<dl class="attribute">
<dt id="django.db.models.DecimalField.max_digits">
<tt class="descclassname">DecimalField.</tt><tt class="descname">max_digits</tt><a class="headerlink" href="#django.db.models.DecimalField.max_digits" title="Permalink to this definition">¶</a></dt>
<dd><p>The maximum number of digits allowed in the number. Note that this number
must be greater than or equal to <tt class="docutils literal"><span class="pre">decimal_places</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.DecimalField.decimal_places">
<tt class="descclassname">DecimalField.</tt><tt class="descname">decimal_places</tt><a class="headerlink" href="#django.db.models.DecimalField.decimal_places" title="Permalink to this definition">¶</a></dt>
<dd><p>The number of decimal places to store with the number.</p>
</dd></dl>

<p>For example, to store numbers up to <tt class="docutils literal"><span class="pre">999</span></tt> with a resolution of 2 decimal
places, you&#8217;d use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">models</span><span class="o">.</span><span class="n">DecimalField</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">max_digits</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">decimal_places</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>And to store numbers up to approximately one billion with a resolution of 10
decimal places:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">models</span><span class="o">.</span><span class="n">DecimalField</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">max_digits</span><span class="o">=</span><span class="mi">19</span><span class="p">,</span> <span class="n">decimal_places</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For more information about the differences between the
<a class="reference internal" href="#django.db.models.FloatField" title="django.db.models.FloatField"><tt class="xref py py-class docutils literal"><span class="pre">FloatField</span></tt></a> and <a class="reference internal" href="#django.db.models.DecimalField" title="django.db.models.DecimalField"><tt class="xref py py-class docutils literal"><span class="pre">DecimalField</span></tt></a> classes, please
see <a class="reference internal" href="#floatfield-vs-decimalfield"><em>FloatField vs. DecimalField</em></a>.</p>
</div>
</div>
<div class="section" id="s-emailfield">
<span id="emailfield"></span><h3><tt class="docutils literal"><span class="pre">EmailField</span></tt><a class="headerlink" href="#emailfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.EmailField">
<em class="property">class </em><tt class="descname">EmailField</tt>(<span class="optional">[</span><em>max_length=75</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.EmailField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> that checks that the value is a valid email address.</p>
<div class="admonition-incompliance-to-rfcs admonition">
<p class="first admonition-title">Incompliance to RFCs</p>
<p class="last">The default 75 character <tt class="docutils literal"><span class="pre">max_length</span></tt> is not capable of storing all
possible RFC3696/5321-compliant email addresses. In order to store all
possible valid email addresses, a <tt class="docutils literal"><span class="pre">max_length</span></tt> of 254 is required.
The default <tt class="docutils literal"><span class="pre">max_length</span></tt> of 75 exists for historical reasons. The
default has not been changed in order to maintain backwards
compatibility with existing uses of <a class="reference internal" href="#django.db.models.EmailField" title="django.db.models.EmailField"><tt class="xref py py-class docutils literal"><span class="pre">EmailField</span></tt></a>.</p>
</div>
</div>
<div class="section" id="s-filefield">
<span id="filefield"></span><h3><tt class="docutils literal"><span class="pre">FileField</span></tt><a class="headerlink" href="#filefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.FileField">
<em class="property">class </em><tt class="descname">FileField</tt>(<span class="optional">[</span><em>upload_to=None</em>, <em>max_length=100</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.FileField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A file-upload field.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <tt class="docutils literal"><span class="pre">primary_key</span></tt> and <tt class="docutils literal"><span class="pre">unique</span></tt> arguments are not supported, and will
raise a <tt class="docutils literal"><span class="pre">TypeError</span></tt> if used.</p>
</div>
<p>Has two optional arguments:</p>
<dl class="attribute">
<dt id="django.db.models.FileField.upload_to">
<tt class="descclassname">FileField.</tt><tt class="descname">upload_to</tt><a class="headerlink" href="#django.db.models.FileField.upload_to" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p><tt class="docutils literal"><span class="pre">upload_to</span></tt> was required in older versions of Django.</p>
</div>
<p>A local filesystem path that will be appended to your <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a>
setting to determine the value of the
<a class="reference internal" href="#django.db.models.fields.files.FieldFile.url" title="django.db.models.fields.files.FieldFile.url"><tt class="xref py py-attr docutils literal"><span class="pre">url</span></tt></a> attribute.</p>
<p>This path may contain <a class="reference external" href="http://docs.python.org/3/library/time.html#time.strftime" title="(in Python v3.4)"><tt class="xref py py-func docutils literal"><span class="pre">strftime()</span></tt></a> formatting, which will be
replaced by the date/time of the file upload (so that uploaded files don&#8217;t
fill up the given directory).</p>
<p>This may also be a callable, such as a function, which will be called to
obtain the upload path, including the filename. This callable must be able
to accept two arguments, and return a Unix-style path (with forward slashes)
to be passed along to the storage system. The two arguments that will be
passed are:</p>
<table class="docutils">
<colgroup>
<col width="32%" />
<col width="68%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Argument</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">instance</span></tt></td>
<td><p class="first">An instance of the model where the
<tt class="docutils literal"><span class="pre">FileField</span></tt> is defined. More specifically,
this is the particular instance where the
current file is being attached.</p>
<p class="last">In most cases, this object will not have been
saved to the database yet, so if it uses the
default <tt class="docutils literal"><span class="pre">AutoField</span></tt>, <em>it might not yet have a
value for its primary key field</em>.</p>
</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">filename</span></tt></td>
<td>The filename that was originally given to the
file. This may or may not be taken into account
when determining the final destination path.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FileField.storage">
<tt class="descclassname">FileField.</tt><tt class="descname">storage</tt><a class="headerlink" href="#django.db.models.FileField.storage" title="Permalink to this definition">¶</a></dt>
<dd><p>A storage object, which handles the storage and retrieval of your
files. See <a class="reference internal" href="../../topics/files.html"><em>Managing files</em></a> for details on how to provide this object.</p>
</dd></dl>

<p>The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.ClearableFileInput" title="django.forms.ClearableFileInput"><tt class="xref py py-class docutils literal"><span class="pre">ClearableFileInput</span></tt></a>.</p>
<p>Using a <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> or an <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> (see below) in a model
takes a few steps:</p>
<ol class="arabic simple">
<li>In your settings file, you&#8217;ll need to define <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a> as the
full path to a directory where you&#8217;d like Django to store uploaded files.
(For performance, these files are not stored in the database.) Define
<a class="reference internal" href="../settings.html#std:setting-MEDIA_URL"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_URL</span></tt></a> as the base public URL of that directory. Make sure
that this directory is writable by the Web server&#8217;s user account.</li>
<li>Add the <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> or <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> to your model, defining
the <a class="reference internal" href="#django.db.models.FileField.upload_to" title="django.db.models.FileField.upload_to"><tt class="xref py py-attr docutils literal"><span class="pre">upload_to</span></tt></a> option to specify a subdirectory of
<a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a> to use for uploaded files.</li>
<li>All that will be stored in your database is a path to the file
(relative to <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a>). You&#8217;ll most likely want to use the
convenience <a class="reference internal" href="#django.db.models.fields.files.FieldFile.url" title="django.db.models.fields.files.FieldFile.url"><tt class="xref py py-attr docutils literal"><span class="pre">url</span></tt></a> attribute
provided by Django. For example, if your <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> is called
<tt class="docutils literal"><span class="pre">mug_shot</span></tt>, you can get the absolute path to your image in a template with
<tt class="docutils literal"><span class="pre">{{</span> <span class="pre">object.mug_shot.url</span> <span class="pre">}}</span></tt>.</li>
</ol>
<p>For example, say your <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a> is set to <tt class="docutils literal"><span class="pre">'/home/media'</span></tt>, and
<a class="reference internal" href="#django.db.models.FileField.upload_to" title="django.db.models.FileField.upload_to"><tt class="xref py py-attr docutils literal"><span class="pre">upload_to</span></tt></a> is set to <tt class="docutils literal"><span class="pre">'photos/%Y/%m/%d'</span></tt>. The <tt class="docutils literal"><span class="pre">'%Y/%m/%d'</span></tt>
part of <a class="reference internal" href="#django.db.models.FileField.upload_to" title="django.db.models.FileField.upload_to"><tt class="xref py py-attr docutils literal"><span class="pre">upload_to</span></tt></a> is <a class="reference external" href="http://docs.python.org/3/library/time.html#time.strftime" title="(in Python v3.4)"><tt class="xref py py-func docutils literal"><span class="pre">strftime()</span></tt></a> formatting;
<tt class="docutils literal"><span class="pre">'%Y'</span></tt> is the four-digit year, <tt class="docutils literal"><span class="pre">'%m'</span></tt> is the two-digit month and <tt class="docutils literal"><span class="pre">'%d'</span></tt> is
the two-digit day. If you upload a file on Jan. 15, 2007, it will be saved in
the directory <tt class="docutils literal"><span class="pre">/home/media/photos/2007/01/15</span></tt>.</p>
<p>If you wanted to retrieve the uploaded file&#8217;s on-disk filename, or the file&#8217;s
size, you could use the <a class="reference internal" href="../files/file.html#django.core.files.File.name" title="django.core.files.File.name"><tt class="xref py py-attr docutils literal"><span class="pre">name</span></tt></a> and
<a class="reference internal" href="../files/file.html#django.core.files.File.size" title="django.core.files.File.size"><tt class="xref py py-attr docutils literal"><span class="pre">size</span></tt></a> attributes respectively; for more
information on the available attributes and methods, see the
<a class="reference internal" href="../files/file.html#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a> class reference and the <a class="reference internal" href="../../topics/files.html"><em>Managing files</em></a>
topic guide.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The file is saved as part of saving the model in the database, so the actual
file name used on disk cannot be relied on until after the model has been
saved.</p>
</div>
<p>The uploaded file&#8217;s relative URL can be obtained using the
<a class="reference internal" href="#django.db.models.fields.files.FieldFile.url" title="django.db.models.fields.files.FieldFile.url"><tt class="xref py py-attr docutils literal"><span class="pre">url</span></tt></a> attribute. Internally,
this calls the <a class="reference internal" href="../files/storage.html#django.core.files.storage.Storage.url" title="django.core.files.storage.Storage.url"><tt class="xref py py-meth docutils literal"><span class="pre">url()</span></tt></a> method of the
underlying <a class="reference internal" href="../files/storage.html#django.core.files.storage.Storage" title="django.core.files.storage.Storage"><tt class="xref py py-class docutils literal"><span class="pre">Storage</span></tt></a> class.</p>
<p id="file-upload-security">Note that whenever you deal with uploaded files, you should pay close attention
to where you&#8217;re uploading them and what type of files they are, to avoid
security holes. <em>Validate all uploaded files</em> so that you&#8217;re sure the files are
what you think they are. For example, if you blindly let somebody upload files,
without validation, to a directory that&#8217;s within your Web server&#8217;s document
root, then somebody could upload a CGI or PHP script and execute that script by
visiting its URL on your site. Don&#8217;t allow that.</p>
<p>Also note that even an uploaded HTML file, since it can be executed by the
browser (though not by the server), can pose security threats that are
equivalent to XSS or CSRF attacks.</p>
<p><a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> instances are created in your database as <tt class="docutils literal"><span class="pre">varchar</span></tt>
columns with a default max length of 100 characters. As with other fields, you
can change the maximum length using the <a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> argument.</p>
<div class="section" id="s-filefield-and-fieldfile">
<span id="filefield-and-fieldfile"></span><h4>FileField and FieldFile<a class="headerlink" href="#filefield-and-fieldfile" title="Permalink to this headline">¶</a></h4>
<dl class="class">
<dt id="django.db.models.fields.files.FieldFile">
<em class="property">class </em><tt class="descname">FieldFile</tt><a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>When you access a <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> on a model, you are
given an instance of <a class="reference internal" href="#django.db.models.fields.files.FieldFile" title="django.db.models.fields.files.FieldFile"><tt class="xref py py-class docutils literal"><span class="pre">FieldFile</span></tt></a> as a proxy for accessing the underlying
file. In addition to the functionality inherited from
<a class="reference internal" href="../files/file.html#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">django.core.files.File</span></tt></a>, this class has several attributes and methods
that can be used to interact with file data:</p>
<dl class="attribute">
<dt id="django.db.models.fields.files.FieldFile.url">
<tt class="descclassname">FieldFile.</tt><tt class="descname">url</tt><a class="headerlink" href="#django.db.models.fields.files.FieldFile.url" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A read-only property to access the file&#8217;s relative URL by calling the
<a class="reference internal" href="../files/storage.html#django.core.files.storage.Storage.url" title="django.core.files.storage.Storage.url"><tt class="xref py py-meth docutils literal"><span class="pre">url()</span></tt></a> method of the underlying
<a class="reference internal" href="../files/storage.html#django.core.files.storage.Storage" title="django.core.files.storage.Storage"><tt class="xref py py-class docutils literal"><span class="pre">Storage</span></tt></a> class.</p>
<dl class="method">
<dt id="django.db.models.fields.files.FieldFile.open">
<tt class="descclassname">FieldFile.</tt><tt class="descname">open</tt>(<em>mode='rb'</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile.open"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile.open" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Behaves like the standard Python <tt class="docutils literal"><span class="pre">open()</span></tt> method and opens the file
associated with this instance in the mode specified by <tt class="docutils literal"><span class="pre">mode</span></tt>.</p>
<dl class="method">
<dt id="django.db.models.fields.files.FieldFile.close">
<tt class="descclassname">FieldFile.</tt><tt class="descname">close</tt>()<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile.close"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile.close" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Behaves like the standard Python <tt class="docutils literal"><span class="pre">file.close()</span></tt> method and closes the file
associated with this instance.</p>
<dl class="method">
<dt id="django.db.models.fields.files.FieldFile.save">
<tt class="descclassname">FieldFile.</tt><tt class="descname">save</tt>(<em>name</em>, <em>content</em>, <em>save=True</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile.save"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile.save" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>This method takes a filename and file contents and passes them to the storage
class for the field, then associates the stored file with the model field.
If you want to manually associate file data with
<a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> instances on your model, the <tt class="docutils literal"><span class="pre">save()</span></tt>
method is used to persist that file data.</p>
<p>Takes two required arguments: <tt class="docutils literal"><span class="pre">name</span></tt> which is the name of the file, and
<tt class="docutils literal"><span class="pre">content</span></tt> which is an object containing the file&#8217;s contents.  The
optional <tt class="docutils literal"><span class="pre">save</span></tt> argument controls whether or not the model instance is
saved after the file associated with this field has been altered. Defaults to
<tt class="docutils literal"><span class="pre">True</span></tt>.</p>
<p>Note that the <tt class="docutils literal"><span class="pre">content</span></tt> argument should be an instance of
<a class="reference internal" href="../files/file.html#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">django.core.files.File</span></tt></a>, not Python&#8217;s built-in file object.
You can construct a <a class="reference internal" href="../files/file.html#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a> from an existing
Python file object like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.core.files</span> <span class="kn">import</span> <span class="n">File</span>
<span class="c"># Open an existing file using Python&#39;s built-in open()</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;/tmp/hello.world&#39;</span><span class="p">)</span>
<span class="n">myfile</span> <span class="o">=</span> <span class="n">File</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
<p>Or you can construct one from a Python string like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.core.files.base</span> <span class="kn">import</span> <span class="n">ContentFile</span>
<span class="n">myfile</span> <span class="o">=</span> <span class="n">ContentFile</span><span class="p">(</span><span class="s">&quot;hello world&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>For more information, see <a class="reference internal" href="../../topics/files.html"><em>Managing files</em></a>.</p>
<dl class="method">
<dt id="django.db.models.fields.files.FieldFile.delete">
<tt class="descclassname">FieldFile.</tt><tt class="descname">delete</tt>(<em>save=True</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile.delete"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile.delete" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Deletes the file associated with this instance and clears all attributes on
the field. Note: This method will close the file if it happens to be open when
<tt class="docutils literal"><span class="pre">delete()</span></tt> is called.</p>
<p>The optional <tt class="docutils literal"><span class="pre">save</span></tt> argument controls whether or not the model instance is
saved after the file associated with this field has been deleted. Defaults to
<tt class="docutils literal"><span class="pre">True</span></tt>.</p>
<p>Note that when a model is deleted, related files are not deleted. If you need
to cleanup orphaned files, you&#8217;ll need to handle it yourself (for instance,
with a custom management command that can be run manually or scheduled to run
periodically via e.g. cron).</p>
</div>
</div>
<div class="section" id="s-filepathfield">
<span id="filepathfield"></span><h3><tt class="docutils literal"><span class="pre">FilePathField</span></tt><a class="headerlink" href="#filepathfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.FilePathField">
<em class="property">class </em><tt class="descname">FilePathField</tt>(<em>path=None</em><span class="optional">[</span>, <em>match=None</em>, <em>recursive=False</em>, <em>max_length=100</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.FilePathField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> whose choices are limited to the filenames in a certain
directory on the filesystem. Has three special arguments, of which the first is
<strong>required</strong>:</p>
<dl class="attribute">
<dt id="django.db.models.FilePathField.path">
<tt class="descclassname">FilePathField.</tt><tt class="descname">path</tt><a class="headerlink" href="#django.db.models.FilePathField.path" title="Permalink to this definition">¶</a></dt>
<dd><p>Required. The absolute filesystem path to a directory from which this
<a class="reference internal" href="#django.db.models.FilePathField" title="django.db.models.FilePathField"><tt class="xref py py-class docutils literal"><span class="pre">FilePathField</span></tt></a> should get its choices. Example: <tt class="docutils literal"><span class="pre">&quot;/home/images&quot;</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FilePathField.match">
<tt class="descclassname">FilePathField.</tt><tt class="descname">match</tt><a class="headerlink" href="#django.db.models.FilePathField.match" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional. A regular expression, as a string, that <a class="reference internal" href="#django.db.models.FilePathField" title="django.db.models.FilePathField"><tt class="xref py py-class docutils literal"><span class="pre">FilePathField</span></tt></a>
will use to filter filenames. Note that the regex will be applied to the
base filename, not the full path. Example: <tt class="docutils literal"><span class="pre">&quot;foo.*\.txt$&quot;</span></tt>, which will
match a file called <tt class="docutils literal"><span class="pre">foo23.txt</span></tt> but not <tt class="docutils literal"><span class="pre">bar.txt</span></tt> or <tt class="docutils literal"><span class="pre">foo23.png</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FilePathField.recursive">
<tt class="descclassname">FilePathField.</tt><tt class="descname">recursive</tt><a class="headerlink" href="#django.db.models.FilePathField.recursive" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional. Either <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>. Default is <tt class="docutils literal"><span class="pre">False</span></tt>. Specifies
whether all subdirectories of <a class="reference internal" href="#django.db.models.FilePathField.path" title="django.db.models.FilePathField.path"><tt class="xref py py-attr docutils literal"><span class="pre">path</span></tt></a> should be included</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FilePathField.allow_files">
<tt class="descclassname">FilePathField.</tt><tt class="descname">allow_files</tt><a class="headerlink" href="#django.db.models.FilePathField.allow_files" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional.  Either <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>.  Default is <tt class="docutils literal"><span class="pre">True</span></tt>.  Specifies
whether files in the specified location should be included.  Either this or
<a class="reference internal" href="#django.db.models.FilePathField.allow_folders" title="django.db.models.FilePathField.allow_folders"><tt class="xref py py-attr docutils literal"><span class="pre">allow_folders</span></tt></a> must be <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FilePathField.allow_folders">
<tt class="descclassname">FilePathField.</tt><tt class="descname">allow_folders</tt><a class="headerlink" href="#django.db.models.FilePathField.allow_folders" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional.  Either <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>.  Default is <tt class="docutils literal"><span class="pre">False</span></tt>.  Specifies
whether folders in the specified location should be included.  Either this
or <a class="reference internal" href="#django.db.models.FilePathField.allow_files" title="django.db.models.FilePathField.allow_files"><tt class="xref py py-attr docutils literal"><span class="pre">allow_files</span></tt></a> must be <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</dd></dl>

<p>Of course, these arguments can be used together.</p>
<p>The one potential gotcha is that <a class="reference internal" href="#django.db.models.FilePathField.match" title="django.db.models.FilePathField.match"><tt class="xref py py-attr docutils literal"><span class="pre">match</span></tt></a> applies to the
base filename, not the full path. So, this example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">FilePathField</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="s">&quot;/home/images&quot;</span><span class="p">,</span> <span class="n">match</span><span class="o">=</span><span class="s">&quot;foo.*&quot;</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>...will match <tt class="docutils literal"><span class="pre">/home/images/foo.png</span></tt> but not <tt class="docutils literal"><span class="pre">/home/images/foo/bar.png</span></tt>
because the <a class="reference internal" href="#django.db.models.FilePathField.match" title="django.db.models.FilePathField.match"><tt class="xref py py-attr docutils literal"><span class="pre">match</span></tt></a> applies to the base filename
(<tt class="docutils literal"><span class="pre">foo.png</span></tt> and <tt class="docutils literal"><span class="pre">bar.png</span></tt>).</p>
<p><a class="reference internal" href="#django.db.models.FilePathField" title="django.db.models.FilePathField"><tt class="xref py py-class docutils literal"><span class="pre">FilePathField</span></tt></a> instances are created in your database as <tt class="docutils literal"><span class="pre">varchar</span></tt>
columns with a default max length of 100 characters. As with other fields, you
can change the maximum length using the <a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> argument.</p>
</div>
<div class="section" id="s-floatfield">
<span id="floatfield"></span><h3><tt class="docutils literal"><span class="pre">FloatField</span></tt><a class="headerlink" href="#floatfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.FloatField">
<em class="property">class </em><tt class="descname">FloatField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.FloatField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A floating-point number represented in Python by a <tt class="docutils literal"><span class="pre">float</span></tt> instance.</p>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</p>
<div class="admonition-floatfield-vs-decimalfield admonition" id="floatfield-vs-decimalfield">
<p class="first admonition-title"><tt class="docutils literal"><span class="pre">FloatField</span></tt> vs. <tt class="docutils literal"><span class="pre">DecimalField</span></tt></p>
<p class="last">The <a class="reference internal" href="#django.db.models.FloatField" title="django.db.models.FloatField"><tt class="xref py py-class docutils literal"><span class="pre">FloatField</span></tt></a> class is sometimes mixed up with the
<a class="reference internal" href="#django.db.models.DecimalField" title="django.db.models.DecimalField"><tt class="xref py py-class docutils literal"><span class="pre">DecimalField</span></tt></a> class. Although they both represent real numbers, they
represent those numbers differently. <tt class="docutils literal"><span class="pre">FloatField</span></tt> uses Python&#8217;s <tt class="docutils literal"><span class="pre">float</span></tt>
type internally, while <tt class="docutils literal"><span class="pre">DecimalField</span></tt> uses Python&#8217;s <tt class="docutils literal"><span class="pre">Decimal</span></tt> type. For
information on the difference between the two, see Python&#8217;s documentation
for the <a class="reference external" href="http://docs.python.org/3/library/decimal.html#module-decimal" title="(in Python v3.4)"><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module.</p>
</div>
</div>
<div class="section" id="s-imagefield">
<span id="imagefield"></span><h3><tt class="docutils literal"><span class="pre">ImageField</span></tt><a class="headerlink" href="#imagefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.ImageField">
<em class="property">class </em><tt class="descname">ImageField</tt>(<span class="optional">[</span><em>upload_to=None</em>, <em>height_field=None</em>, <em>width_field=None</em>, <em>max_length=100</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.ImageField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Inherits all attributes and methods from <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a>, but also
validates that the uploaded object is a valid image.</p>
<p>In addition to the special attributes that are available for <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a>,
an <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> also has <tt class="docutils literal"><span class="pre">height</span></tt> and <tt class="docutils literal"><span class="pre">width</span></tt> attributes.</p>
<p>To facilitate querying on those attributes, <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> has two extra
optional arguments:</p>
<dl class="attribute">
<dt id="django.db.models.ImageField.height_field">
<tt class="descclassname">ImageField.</tt><tt class="descname">height_field</tt><a class="headerlink" href="#django.db.models.ImageField.height_field" title="Permalink to this definition">¶</a></dt>
<dd><p>Name of a model field which will be auto-populated with the height of the
image each time the model instance is saved.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ImageField.width_field">
<tt class="descclassname">ImageField.</tt><tt class="descname">width_field</tt><a class="headerlink" href="#django.db.models.ImageField.width_field" title="Permalink to this definition">¶</a></dt>
<dd><p>Name of a model field which will be auto-populated with the width of the
image each time the model instance is saved.</p>
</dd></dl>

<p>Requires the <a class="reference external" href="http://pillow.readthedocs.org/en/latest/">Pillow</a> library.</p>
<p><a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> instances are created in your database as <tt class="docutils literal"><span class="pre">varchar</span></tt>
columns with a default max length of 100 characters. As with other fields, you
can change the maximum length using the <a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> argument.</p>
<p>The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.ClearableFileInput" title="django.forms.ClearableFileInput"><tt class="xref py py-class docutils literal"><span class="pre">ClearableFileInput</span></tt></a>.</p>
</div>
<div class="section" id="s-integerfield">
<span id="integerfield"></span><h3><tt class="docutils literal"><span class="pre">IntegerField</span></tt><a class="headerlink" href="#integerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.IntegerField">
<em class="property">class </em><tt class="descname">IntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.IntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An integer. Values from <tt class="docutils literal"><span class="pre">-2147483648</span></tt> to <tt class="docutils literal"><span class="pre">2147483647</span></tt> are safe in all
databases supported by Django. The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</p>
</div>
<div class="section" id="s-ipaddressfield">
<span id="ipaddressfield"></span><h3><tt class="docutils literal"><span class="pre">IPAddressField</span></tt><a class="headerlink" href="#ipaddressfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.IPAddressField">
<em class="property">class </em><tt class="descname">IPAddressField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.IPAddressField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.7: </span>This field has been deprecated in favor of
<a class="reference internal" href="#django.db.models.GenericIPAddressField" title="django.db.models.GenericIPAddressField"><tt class="xref py py-class docutils literal"><span class="pre">GenericIPAddressField</span></tt></a>.</p>
</div>
<p>An IP address, in string format (e.g. &#8220;192.0.2.30&#8221;). The default form widget
for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</p>
</div>
<div class="section" id="s-genericipaddressfield">
<span id="genericipaddressfield"></span><h3><tt class="docutils literal"><span class="pre">GenericIPAddressField</span></tt><a class="headerlink" href="#genericipaddressfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.GenericIPAddressField">
<em class="property">class </em><tt class="descname">GenericIPAddressField</tt>(<span class="optional">[</span><em>protocol=both</em>, <em>unpack_ipv4=False</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.GenericIPAddressField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An IPv4 or IPv6 address, in string format (e.g. <tt class="docutils literal"><span class="pre">192.0.2.30</span></tt> or
<tt class="docutils literal"><span class="pre">2a02:42fe::4</span></tt>). The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</p>
<p>The IPv6 address normalization follows <span class="target" id="index-0"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc4291.html#section-2.2"><strong>RFC 4291</strong></a> section 2.2,
including using the IPv4 format suggested in paragraph 3 of that section, like
<tt class="docutils literal"><span class="pre">::ffff:192.0.2.0</span></tt>. For example, <tt class="docutils literal"><span class="pre">2001:0::0:01</span></tt> would be normalized to
<tt class="docutils literal"><span class="pre">2001::1</span></tt>, and <tt class="docutils literal"><span class="pre">::ffff:0a0a:0a0a</span></tt> to <tt class="docutils literal"><span class="pre">::ffff:10.10.10.10</span></tt>. All characters
are converted to lowercase.</p>
<dl class="attribute">
<dt id="django.db.models.GenericIPAddressField.protocol">
<tt class="descclassname">GenericIPAddressField.</tt><tt class="descname">protocol</tt><a class="headerlink" href="#django.db.models.GenericIPAddressField.protocol" title="Permalink to this definition">¶</a></dt>
<dd><p>Limits valid inputs to the specified protocol.
Accepted values are <tt class="docutils literal"><span class="pre">'both'</span></tt> (default), <tt class="docutils literal"><span class="pre">'IPv4'</span></tt>
or <tt class="docutils literal"><span class="pre">'IPv6'</span></tt>. Matching is case insensitive.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.GenericIPAddressField.unpack_ipv4">
<tt class="descclassname">GenericIPAddressField.</tt><tt class="descname">unpack_ipv4</tt><a class="headerlink" href="#django.db.models.GenericIPAddressField.unpack_ipv4" title="Permalink to this definition">¶</a></dt>
<dd><p>Unpacks IPv4 mapped addresses like <tt class="docutils literal"><span class="pre">::ffff:192.0.2.1</span></tt>.
If this option is enabled that address would be unpacked to
<tt class="docutils literal"><span class="pre">192.0.2.1</span></tt>. Default is disabled. Can only be used
when <tt class="docutils literal"><span class="pre">protocol</span></tt> is set to <tt class="docutils literal"><span class="pre">'both'</span></tt>.</p>
</dd></dl>

<p>If you allow for blank values, you have to allow for null values since blank
values are stored as null.</p>
</div>
<div class="section" id="s-nullbooleanfield">
<span id="nullbooleanfield"></span><h3><tt class="docutils literal"><span class="pre">NullBooleanField</span></tt><a class="headerlink" href="#nullbooleanfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.NullBooleanField">
<em class="property">class </em><tt class="descname">NullBooleanField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.NullBooleanField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Like a <a class="reference internal" href="#django.db.models.BooleanField" title="django.db.models.BooleanField"><tt class="xref py py-class docutils literal"><span class="pre">BooleanField</span></tt></a>, but allows <tt class="docutils literal"><span class="pre">NULL</span></tt> as one of the options. Use
this instead of a <a class="reference internal" href="#django.db.models.BooleanField" title="django.db.models.BooleanField"><tt class="xref py py-class docutils literal"><span class="pre">BooleanField</span></tt></a> with <tt class="docutils literal"><span class="pre">null=True</span></tt>. The default form
widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.NullBooleanSelect" title="django.forms.NullBooleanSelect"><tt class="xref py py-class docutils literal"><span class="pre">NullBooleanSelect</span></tt></a>.</p>
</div>
<div class="section" id="s-positiveintegerfield">
<span id="positiveintegerfield"></span><h3><tt class="docutils literal"><span class="pre">PositiveIntegerField</span></tt><a class="headerlink" href="#positiveintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.PositiveIntegerField">
<em class="property">class </em><tt class="descname">PositiveIntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.PositiveIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Like an <a class="reference internal" href="#django.db.models.IntegerField" title="django.db.models.IntegerField"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a>, but must be either positive or zero (<tt class="docutils literal"><span class="pre">0</span></tt>).
Values from <tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">2147483647</span></tt> are safe in all databases supported by
Django. The value <tt class="docutils literal"><span class="pre">0</span></tt> is accepted for backward compatibility reasons.</p>
</div>
<div class="section" id="s-positivesmallintegerfield">
<span id="positivesmallintegerfield"></span><h3><tt class="docutils literal"><span class="pre">PositiveSmallIntegerField</span></tt><a class="headerlink" href="#positivesmallintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.PositiveSmallIntegerField">
<em class="property">class </em><tt class="descname">PositiveSmallIntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.PositiveSmallIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Like a <a class="reference internal" href="#django.db.models.PositiveIntegerField" title="django.db.models.PositiveIntegerField"><tt class="xref py py-class docutils literal"><span class="pre">PositiveIntegerField</span></tt></a>, but only allows values under a certain
(database-dependent) point. Values from <tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">32767</span></tt> are safe in all
databases supported by Django.</p>
</div>
<div class="section" id="s-slugfield">
<span id="slugfield"></span><h3><tt class="docutils literal"><span class="pre">SlugField</span></tt><a class="headerlink" href="#slugfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.SlugField">
<em class="property">class </em><tt class="descname">SlugField</tt>(<span class="optional">[</span><em>max_length=50</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.SlugField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p><a class="reference internal" href="../../glossary.html#term-slug"><em class="xref std std-term">Slug</em></a> is a newspaper term. A slug is a short label for something,
containing only letters, numbers, underscores or hyphens. They&#8217;re generally used
in URLs.</p>
<p>Like a CharField, you can specify <a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> (read the note
about database portability and <a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> in that section,
too). If <a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> is not specified, Django will use a
default length of 50.</p>
<p>Implies setting <a class="reference internal" href="#django.db.models.Field.db_index" title="django.db.models.Field.db_index"><tt class="xref py py-attr docutils literal"><span class="pre">Field.db_index</span></tt></a> to <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
<p>It is often useful to automatically prepopulate a SlugField based on the value
of some other value.  You can do this automatically in the admin using
<a class="reference internal" href="../contrib/admin/index.html#django.contrib.admin.ModelAdmin.prepopulated_fields" title="django.contrib.admin.ModelAdmin.prepopulated_fields"><tt class="xref py py-attr docutils literal"><span class="pre">prepopulated_fields</span></tt></a>.</p>
</div>
<div class="section" id="s-smallintegerfield">
<span id="smallintegerfield"></span><h3><tt class="docutils literal"><span class="pre">SmallIntegerField</span></tt><a class="headerlink" href="#smallintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.SmallIntegerField">
<em class="property">class </em><tt class="descname">SmallIntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.SmallIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Like an <a class="reference internal" href="#django.db.models.IntegerField" title="django.db.models.IntegerField"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a>, but only allows values under a certain
(database-dependent) point. Values from <tt class="docutils literal"><span class="pre">-32768</span></tt> to <tt class="docutils literal"><span class="pre">32767</span></tt> are safe in all
databases supported by Django.</p>
</div>
<div class="section" id="s-textfield">
<span id="textfield"></span><h3><tt class="docutils literal"><span class="pre">TextField</span></tt><a class="headerlink" href="#textfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.TextField">
<em class="property">class </em><tt class="descname">TextField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.TextField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A large text field. The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.Textarea" title="django.forms.Textarea"><tt class="xref py py-class docutils literal"><span class="pre">Textarea</span></tt></a>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>If you specify a <tt class="docutils literal"><span class="pre">max_length</span></tt> attribute, it will be reflected in the
<a class="reference internal" href="../forms/widgets.html#django.forms.Textarea" title="django.forms.Textarea"><tt class="xref py py-class docutils literal"><span class="pre">Textarea</span></tt></a> widget of the auto-generated form field.
However it is not enforced at the model or database level. Use a
<a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> for that.</p>
</div>
<div class="admonition-mysql-users admonition">
<p class="first admonition-title">MySQL users</p>
<p class="last">If you are using this field with MySQLdb 1.2.1p2 and the <tt class="docutils literal"><span class="pre">utf8_bin</span></tt>
collation (which is <em>not</em> the default), there are some issues to be aware
of. Refer to the <a class="reference internal" href="../databases.html#mysql-collation"><em>MySQL database notes</em></a> for
details.</p>
</div>
</div>
<div class="section" id="s-timefield">
<span id="timefield"></span><h3><tt class="docutils literal"><span class="pre">TimeField</span></tt><a class="headerlink" href="#timefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.TimeField">
<em class="property">class </em><tt class="descname">TimeField</tt>(<span class="optional">[</span><em>auto_now=False</em>, <em>auto_now_add=False</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.TimeField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A time, represented in Python by a <tt class="docutils literal"><span class="pre">datetime.time</span></tt> instance. Accepts the same
auto-population options as <a class="reference internal" href="#django.db.models.DateField" title="django.db.models.DateField"><tt class="xref py py-class docutils literal"><span class="pre">DateField</span></tt></a>.</p>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.
The admin adds some JavaScript shortcuts.</p>
</div>
<div class="section" id="s-urlfield">
<span id="urlfield"></span><h3><tt class="docutils literal"><span class="pre">URLField</span></tt><a class="headerlink" href="#urlfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.URLField">
<em class="property">class </em><tt class="descname">URLField</tt>(<span class="optional">[</span><em>max_length=200</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.URLField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> for a URL.</p>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><tt class="xref py py-class docutils literal"><span class="pre">TextInput</span></tt></a>.</p>
<p>Like all <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> subclasses, <a class="reference internal" href="#django.db.models.URLField" title="django.db.models.URLField"><tt class="xref py py-class docutils literal"><span class="pre">URLField</span></tt></a> takes the optional
<a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> argument. If you don&#8217;t specify
<a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a>, a default of 200 is used.</p>
</div>
</div>
<div class="section" id="s-module-django.db.models.fields.related">
<span id="s-relationship-fields"></span><span id="module-django.db.models.fields.related"></span><span id="relationship-fields"></span><h2>Relationship fields<a class="headerlink" href="#module-django.db.models.fields.related" title="Permalink to this headline">¶</a></h2>
<p>Django also defines a set of fields that represent relations.</p>
<div class="section" id="s-foreignkey">
<span id="s-ref-foreignkey"></span><span id="foreignkey"></span><span id="ref-foreignkey"></span><h3><tt class="docutils literal"><span class="pre">ForeignKey</span></tt><a class="headerlink" href="#foreignkey" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.ForeignKey">
<em class="property">class </em><tt class="descname">ForeignKey</tt>(<em>othermodel</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.ForeignKey" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A many-to-one relationship. Requires a positional argument: the class to which
the model is related.</p>
<p id="recursive-relationships">To create a recursive relationship &#8211; an object that has a many-to-one
relationship with itself &#8211; use <tt class="docutils literal"><span class="pre">models.ForeignKey('self')</span></tt>.</p>
<p id="lazy-relationships">If you need to create a relationship on a model that has not yet been defined,
you can use the name of the model, rather than the model object itself:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Car</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">manufacturer</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;Manufacturer&#39;</span><span class="p">)</span>
    <span class="c"># ...</span>

<span class="k">class</span> <span class="nc">Manufacturer</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c"># ...</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>To refer to models defined in another application, you can explicitly specify
a model with the full application label. For example, if the <tt class="docutils literal"><span class="pre">Manufacturer</span></tt>
model above is defined in another application called <tt class="docutils literal"><span class="pre">production</span></tt>, you&#8217;d
need to use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Car</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">manufacturer</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;production.Manufacturer&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>This sort of reference can be useful when resolving circular import
dependencies between two applications.</p>
<p>A database index is automatically created on the <tt class="docutils literal"><span class="pre">ForeignKey</span></tt>. You can
disable this by setting <a class="reference internal" href="#django.db.models.Field.db_index" title="django.db.models.Field.db_index"><tt class="xref py py-attr docutils literal"><span class="pre">db_index</span></tt></a> to <tt class="docutils literal"><span class="pre">False</span></tt>.  You may want to
avoid the overhead of an index if you are creating a foreign key for
consistency rather than joins, or if you will be creating an alternative index
like a partial or multiple column index.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">It is not recommended to have a <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> from an app without migrations
to an app with migrations. See the <a class="reference internal" href="../../topics/migrations.html#unmigrated-dependencies"><em>dependencies documentation</em></a> for more details.</p>
</div>
<div class="section" id="s-database-representation">
<span id="database-representation"></span><h4>Database Representation<a class="headerlink" href="#database-representation" title="Permalink to this headline">¶</a></h4>
<p>Behind the scenes, Django appends <tt class="docutils literal"><span class="pre">&quot;_id&quot;</span></tt> to the field name to create its
database column name. In the above example, the database table for the <tt class="docutils literal"><span class="pre">Car</span></tt>
model will have a <tt class="docutils literal"><span class="pre">manufacturer_id</span></tt> column. (You can change this explicitly by
specifying <a class="reference internal" href="#django.db.models.Field.db_column" title="django.db.models.Field.db_column"><tt class="xref py py-attr docutils literal"><span class="pre">db_column</span></tt></a>) However, your code should never have to
deal with the database column name, unless you write custom SQL. You&#8217;ll always
deal with the field names of your model object.</p>
</div>
<div class="section" id="s-arguments">
<span id="s-foreign-key-arguments"></span><span id="arguments"></span><span id="foreign-key-arguments"></span><h4>Arguments<a class="headerlink" href="#arguments" title="Permalink to this headline">¶</a></h4>
<p><a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> accepts an extra set of arguments &#8211; all optional &#8211; that
define the details of how the relation works.</p>
<dl class="attribute">
<dt id="django.db.models.ForeignKey.limit_choices_to">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">limit_choices_to</tt><a class="headerlink" href="#django.db.models.ForeignKey.limit_choices_to" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets a limit to the available choices for this field when this field is
rendered using a <tt class="docutils literal"><span class="pre">ModelForm</span></tt> or the admin (by default, all objects
in the queryset are available to choose). Either a dictionary, a
<a class="reference internal" href="queries.html#django.db.models.Q" title="django.db.models.Q"><tt class="xref py py-class docutils literal"><span class="pre">Q</span></tt></a> object, or a callable returning a
dictionary or <a class="reference internal" href="queries.html#django.db.models.Q" title="django.db.models.Q"><tt class="xref py py-class docutils literal"><span class="pre">Q</span></tt></a> object can be used.</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">staff_member</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">limit_choices_to</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;is_staff&#39;</span><span class="p">:</span> <span class="bp">True</span><span class="p">})</span>
</pre></div>
</div>
<p>causes the corresponding field on the <tt class="docutils literal"><span class="pre">ModelForm</span></tt> to list only <tt class="docutils literal"><span class="pre">Users</span></tt>
that have <tt class="docutils literal"><span class="pre">is_staff=True</span></tt>. This may be helpful in the Django admin.</p>
<p>The callable form can be helpful, for instance, when used in conjunction
with the Python <tt class="docutils literal"><span class="pre">datetime</span></tt> module to limit selections by date range. For
example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">limit_pub_date_choices</span><span class="p">():</span>
    <span class="k">return</span> <span class="p">{</span><span class="s">&#39;pub_date__lte&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()}</span>

<span class="n">limit_choices_to</span> <span class="o">=</span> <span class="n">limit_pub_date_choices</span>
</pre></div>
</div>
<p>If <tt class="docutils literal"><span class="pre">limit_choices_to</span></tt> is or returns a <a class="reference internal" href="queries.html#django.db.models.Q" title="django.db.models.Q"><tt class="xref py py-class docutils literal"><span class="pre">Q</span> <span class="pre">object</span></tt></a>, which is useful for <a class="reference internal" href="../../topics/db/queries.html#complex-lookups-with-q"><em>complex queries</em></a>, then it will only have an effect on the choices
available in the admin when the field is not listed in
<a class="reference internal" href="../contrib/admin/index.html#django.contrib.admin.ModelAdmin.raw_id_fields" title="django.contrib.admin.ModelAdmin.raw_id_fields"><tt class="xref py py-attr docutils literal"><span class="pre">raw_id_fields</span></tt></a> in the
<tt class="docutils literal"><span class="pre">ModelAdmin</span></tt> for the model.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>Previous versions of Django do not allow passing a callable as a value
for <tt class="docutils literal"><span class="pre">limit_choices_to</span></tt>.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If a callable is used for <tt class="docutils literal"><span class="pre">limit_choices_to</span></tt>, it will be invoked
every time a new form is instantiated. It may also be invoked when a
model is validated, for example by management commands or the admin.
The admin constructs querysets to validate its form inputs in various
edge cases multiple times, so there is a possibility your callable may
be invoked several times.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.related_name">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">related_name</tt><a class="headerlink" href="#django.db.models.ForeignKey.related_name" title="Permalink to this definition">¶</a></dt>
<dd><p>The name to use for the relation from the related object back to this one.
It&#8217;s also the default value for <a class="reference internal" href="#django.db.models.ForeignKey.related_query_name" title="django.db.models.ForeignKey.related_query_name"><tt class="xref py py-attr docutils literal"><span class="pre">related_query_name</span></tt></a> (the name to use
for the reverse filter name from the target model). See the <a class="reference internal" href="../../topics/db/queries.html#backwards-related-objects"><em>related
objects documentation</em></a> for a full explanation
and example. Note that you must set this value when defining relations on
<a class="reference internal" href="../../topics/db/models.html#abstract-base-classes"><em>abstract models</em></a>; and when you do so
<a class="reference internal" href="../../topics/db/models.html#abstract-related-name"><em>some special syntax</em></a> is available.</p>
<p>If you&#8217;d prefer Django not to create a backwards relation, set
<tt class="docutils literal"><span class="pre">related_name</span></tt> to <tt class="docutils literal"><span class="pre">'+'</span></tt> or end it with <tt class="docutils literal"><span class="pre">'+'</span></tt>. For example, this will
ensure that the <tt class="docutils literal"><span class="pre">User</span></tt> model won&#8217;t have a backwards relation to this
model:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&#39;+&#39;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.related_query_name">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">related_query_name</tt><a class="headerlink" href="#django.db.models.ForeignKey.related_query_name" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.6.</span> </div>
<p>The name to use for the reverse filter name from the target model.
Defaults to the value of <a class="reference internal" href="#django.db.models.ForeignKey.related_name" title="django.db.models.ForeignKey.related_name"><tt class="xref py py-attr docutils literal"><span class="pre">related_name</span></tt></a> if it is set, otherwise it
defaults to the name of the model:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Declare the ForeignKey with related_query_name</span>
<span class="k">class</span> <span class="nc">Tag</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">article</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Article</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&quot;tags&quot;</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="s">&quot;tag&quot;</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">255</span><span class="p">)</span>

<span class="c"># That&#39;s now the name of the reverse filter</span>
<span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">tag__name</span><span class="o">=</span><span class="s">&quot;important&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.to_field">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">to_field</tt><a class="headerlink" href="#django.db.models.ForeignKey.to_field" title="Permalink to this definition">¶</a></dt>
<dd><p>The field on the related object that the relation is to. By default, Django
uses the primary key of the related object.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.db_constraint">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">db_constraint</tt><a class="headerlink" href="#django.db.models.ForeignKey.db_constraint" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.6.</span> </div>
<p>Controls whether or not a constraint should be created in the database for
this foreign key. The default is <tt class="docutils literal"><span class="pre">True</span></tt>, and that&#8217;s almost certainly what
you want; setting this to <tt class="docutils literal"><span class="pre">False</span></tt> can be very bad for data integrity.
That said, here are some scenarios where you might want to do this:</p>
<ul class="simple">
<li>You have legacy data that is not valid.</li>
<li>You&#8217;re sharding your database.</li>
</ul>
<p>If this is set to <tt class="docutils literal"><span class="pre">False</span></tt>, accessing a related object that doesn&#8217;t exist
will raise its <tt class="docutils literal"><span class="pre">DoesNotExist</span></tt> exception.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.on_delete">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">on_delete</tt><a class="headerlink" href="#django.db.models.ForeignKey.on_delete" title="Permalink to this definition">¶</a></dt>
<dd><p>When an object referenced by a <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> is deleted, Django by
default emulates the behavior of the SQL constraint <tt class="docutils literal"><span class="pre">ON</span> <span class="pre">DELETE</span> <span class="pre">CASCADE</span></tt>
and also deletes the object containing the <tt class="docutils literal"><span class="pre">ForeignKey</span></tt>. This behavior
can be overridden by specifying the <a class="reference internal" href="#django.db.models.ForeignKey.on_delete" title="django.db.models.ForeignKey.on_delete"><tt class="xref py py-attr docutils literal"><span class="pre">on_delete</span></tt></a> argument. For
example, if you have a nullable <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> and you want it to be
set null when the referenced object is deleted:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">SET_NULL</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<p>The possible values for <a class="reference internal" href="#django.db.models.ForeignKey.on_delete" title="django.db.models.ForeignKey.on_delete"><tt class="xref py py-attr docutils literal"><span class="pre">on_delete</span></tt></a> are found in
<a class="reference internal" href="../../topics/db/models.html#module-django.db.models" title="django.db.models"><tt class="xref py py-mod docutils literal"><span class="pre">django.db.models</span></tt></a>:</p>
<ul>
<li><dl class="attribute">
<dt id="django.db.models.CASCADE">
<tt class="descname">CASCADE</tt><a class="headerlink" href="#django.db.models.CASCADE" title="Permalink to this definition">¶</a></dt>
<dd><p>Cascade deletes; the default.</p>
</dd></dl>

</li>
<li><dl class="attribute">
<dt id="django.db.models.PROTECT">
<tt class="descname">PROTECT</tt><a class="headerlink" href="#django.db.models.PROTECT" title="Permalink to this definition">¶</a></dt>
<dd><p>Prevent deletion of the referenced object by raising
<a class="reference internal" href="../exceptions.html#django.db.models.ProtectedError" title="django.db.models.ProtectedError"><tt class="xref py py-exc docutils literal"><span class="pre">ProtectedError</span></tt></a>, a subclass of
<a class="reference internal" href="../exceptions.html#django.db.IntegrityError" title="django.db.IntegrityError"><tt class="xref py py-exc docutils literal"><span class="pre">django.db.IntegrityError</span></tt></a>.</p>
</dd></dl>

</li>
<li><dl class="attribute">
<dt id="django.db.models.SET_NULL">
<tt class="descname">SET_NULL</tt><a class="headerlink" href="#django.db.models.SET_NULL" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> null; this is only possible if
<a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a> is <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</dd></dl>

</li>
<li><dl class="attribute">
<dt id="django.db.models.SET_DEFAULT">
<tt class="descname">SET_DEFAULT</tt><a class="headerlink" href="#django.db.models.SET_DEFAULT" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> to its default value; a default for the
<a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> must be set.</p>
</dd></dl>

</li>
<li><dl class="function">
<dt id="django.db.models.SET">
<tt class="descname">SET</tt>()<a class="headerlink" href="#django.db.models.SET" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> to the value passed to
<a class="reference internal" href="#django.db.models.SET" title="django.db.models.SET"><tt class="xref py py-func docutils literal"><span class="pre">SET()</span></tt></a>, or if a callable is passed in,
the result of calling it. In most cases, passing a callable will be
necessary to avoid executing queries at the time your models.py is
imported:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth</span> <span class="kn">import</span> <span class="n">get_user_model</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">def</span> <span class="nf">get_sentinel_user</span><span class="p">():</span>
    <span class="k">return</span> <span class="n">get_user_model</span><span class="p">()</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_or_create</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="s">&#39;deleted&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">,</span>
                             <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">SET</span><span class="p">(</span><span class="n">get_sentinel_user</span><span class="p">))</span>
</pre></div>
</div>
</dd></dl>

</li>
<li><dl class="attribute">
<dt id="django.db.models.DO_NOTHING">
<tt class="descname">DO_NOTHING</tt><a class="headerlink" href="#django.db.models.DO_NOTHING" title="Permalink to this definition">¶</a></dt>
<dd><p>Take no action. If your database backend enforces referential
integrity, this will cause an <a class="reference internal" href="../exceptions.html#django.db.IntegrityError" title="django.db.IntegrityError"><tt class="xref py py-exc docutils literal"><span class="pre">IntegrityError</span></tt></a> unless
you manually add an SQL <tt class="docutils literal"><span class="pre">ON</span> <span class="pre">DELETE</span></tt> constraint to the database field
(perhaps using <a class="reference internal" href="../../howto/initial-data.html#initial-sql"><em>initial sql</em></a>).</p>
</dd></dl>

</li>
</ul>
<dl class="attribute">
<dt id="django.db.models.ForeignKey.swappable">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">swappable</tt><a class="headerlink" href="#django.db.models.ForeignKey.swappable" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Controls the migration framework&#8217;s reaction if this <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>
is pointing at a swappable model. If it is <tt class="docutils literal"><span class="pre">True</span></tt> - the default -
then if the <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> is pointing at a model which matches
the current value of <tt class="docutils literal"><span class="pre">settings.AUTH_USER_MODEL</span></tt> (or another swappable
model setting) the relationship will be stored in the migration using
a reference to the setting, not to the model directly.</p>
<p>You only want to override this to be <tt class="docutils literal"><span class="pre">False</span></tt> if you are sure your
model should always point towards the swapped-in model - for example,
if it is a profile model designed specifically for your custom user model.</p>
<p>Setting it to <tt class="docutils literal"><span class="pre">False</span></tt> does not mean you can reference a swappable model
even if it is swapped out - <tt class="docutils literal"><span class="pre">False</span></tt> just means that the migrations made
with this ForeignKey will always reference the exact model you specify
(so it will fail hard if the user tries to run with a User model you don&#8217;t
support, for example).</p>
<p>If in doubt, leave it to its default of <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-manytomanyfield">
<span id="s-ref-manytomany"></span><span id="manytomanyfield"></span><span id="ref-manytomany"></span><h3><tt class="docutils literal"><span class="pre">ManyToManyField</span></tt><a class="headerlink" href="#manytomanyfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.ManyToManyField">
<em class="property">class </em><tt class="descname">ManyToManyField</tt>(<em>othermodel</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.ManyToManyField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A many-to-many relationship. Requires a positional argument: the class to
which the model is related, which works exactly the same as it does for
<a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>, including <a class="reference internal" href="#recursive-relationships"><em>recursive</em></a> and
<a class="reference internal" href="#lazy-relationships"><em>lazy</em></a> relationships.</p>
<p>Related objects can be added, removed, or created with the field&#8217;s
<a class="reference internal" href="relations.html#django.db.models.fields.related.RelatedManager" title="django.db.models.fields.related.RelatedManager"><tt class="xref py py-class docutils literal"><span class="pre">RelatedManager</span></tt></a>.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">It is not recommended to have a <tt class="docutils literal"><span class="pre">ManyToManyField</span></tt> from an app without migrations
to an app with migrations. See the <a class="reference internal" href="../../topics/migrations.html#unmigrated-dependencies"><em>dependencies documentation</em></a> for more details.</p>
</div>
<div class="section" id="s-id1">
<span id="id1"></span><h4>Database Representation<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h4>
<p>Behind the scenes, Django creates an intermediary join table to represent the
many-to-many relationship. By default, this table name is generated using the
name of the many-to-many field and the name of the table for the model that
contains it. Since some databases don&#8217;t support table names above a certain
length, these table names will be automatically truncated to 64 characters and a
uniqueness hash will be used. This means you might see table names like
<tt class="docutils literal"><span class="pre">author_books_9cdf4</span></tt>; this is perfectly normal.  You can manually provide the
name of the join table using the <a class="reference internal" href="#django.db.models.ManyToManyField.db_table" title="django.db.models.ManyToManyField.db_table"><tt class="xref py py-attr docutils literal"><span class="pre">db_table</span></tt></a> option.</p>
</div>
<div class="section" id="s-manytomany-arguments">
<span id="s-id2"></span><span id="manytomany-arguments"></span><span id="id2"></span><h4>Arguments<a class="headerlink" href="#manytomany-arguments" title="Permalink to this headline">¶</a></h4>
<p><a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> accepts an extra set of arguments &#8211; all optional &#8211;
that control how the relationship functions.</p>
<dl class="attribute">
<dt id="django.db.models.ManyToManyField.related_name">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">related_name</tt><a class="headerlink" href="#django.db.models.ManyToManyField.related_name" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#django.db.models.ForeignKey.related_name" title="django.db.models.ForeignKey.related_name"><tt class="xref py py-attr docutils literal"><span class="pre">ForeignKey.related_name</span></tt></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.related_query_name">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">related_query_name</tt><a class="headerlink" href="#django.db.models.ManyToManyField.related_query_name" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.6.</span> </div>
<p>Same as <a class="reference internal" href="#django.db.models.ForeignKey.related_query_name" title="django.db.models.ForeignKey.related_query_name"><tt class="xref py py-attr docutils literal"><span class="pre">ForeignKey.related_query_name</span></tt></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.limit_choices_to">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">limit_choices_to</tt><a class="headerlink" href="#django.db.models.ManyToManyField.limit_choices_to" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#django.db.models.ForeignKey.limit_choices_to" title="django.db.models.ForeignKey.limit_choices_to"><tt class="xref py py-attr docutils literal"><span class="pre">ForeignKey.limit_choices_to</span></tt></a>.</p>
<p><tt class="docutils literal"><span class="pre">limit_choices_to</span></tt> has no effect when used on a <tt class="docutils literal"><span class="pre">ManyToManyField</span></tt> with a
custom intermediate table specified using the
<a class="reference internal" href="#django.db.models.ManyToManyField.through" title="django.db.models.ManyToManyField.through"><tt class="xref py py-attr docutils literal"><span class="pre">through</span></tt></a> parameter.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.symmetrical">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">symmetrical</tt><a class="headerlink" href="#django.db.models.ManyToManyField.symmetrical" title="Permalink to this definition">¶</a></dt>
<dd><p>Only used in the definition of ManyToManyFields on self. Consider the
following model:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">friends</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="s">&quot;self&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>When Django processes this model, it identifies that it has a
<a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> on itself, and as a result, it doesn&#8217;t add a
<tt class="docutils literal"><span class="pre">person_set</span></tt> attribute to the <tt class="docutils literal"><span class="pre">Person</span></tt> class. Instead, the
<a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> is assumed to be symmetrical &#8211; that is, if I am
your friend, then you are my friend.</p>
<p>If you do not want symmetry in many-to-many relationships with <tt class="docutils literal"><span class="pre">self</span></tt>, set
<a class="reference internal" href="#django.db.models.ManyToManyField.symmetrical" title="django.db.models.ManyToManyField.symmetrical"><tt class="xref py py-attr docutils literal"><span class="pre">symmetrical</span></tt></a> to <tt class="docutils literal"><span class="pre">False</span></tt>. This will force Django to
add the descriptor for the reverse relationship, allowing
<a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> relationships to be non-symmetrical.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.through">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">through</tt><a class="headerlink" href="#django.db.models.ManyToManyField.through" title="Permalink to this definition">¶</a></dt>
<dd><p>Django will automatically generate a table to manage many-to-many
relationships. However, if you want to manually specify the intermediary
table, you can use the <a class="reference internal" href="#django.db.models.ManyToManyField.through" title="django.db.models.ManyToManyField.through"><tt class="xref py py-attr docutils literal"><span class="pre">through</span></tt></a> option to specify
the Django model that represents the intermediate table that you want to
use.</p>
<p>The most common use for this option is when you want to associate
<a class="reference internal" href="../../topics/db/models.html#intermediary-manytomany"><em>extra data with a many-to-many relationship</em></a>.</p>
<p>If you don&#8217;t specify an explicit <tt class="docutils literal"><span class="pre">through</span></tt> model, there is still an
implicit <tt class="docutils literal"><span class="pre">through</span></tt> model class you can use to directly access the table
created to hold the association. It has three fields to link the models.</p>
<p>If the source and target models differ, the following fields are
generated:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">id</span></tt>: the primary key of the relation.</li>
<li><tt class="docutils literal"><span class="pre">&lt;containing_model&gt;_id</span></tt>: the <tt class="docutils literal"><span class="pre">id</span></tt> of the model that declares the
<tt class="docutils literal"><span class="pre">ManyToManyField</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">&lt;other_model&gt;_id</span></tt>: the <tt class="docutils literal"><span class="pre">id</span></tt> of the model that the
<tt class="docutils literal"><span class="pre">ManyToManyField</span></tt> points to.</li>
</ul>
<p>If the <tt class="docutils literal"><span class="pre">ManyToManyField</span></tt> points from and to the same model, the following
fields are generated:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">id</span></tt>: the primary key of the relation.</li>
<li><tt class="docutils literal"><span class="pre">from_&lt;model&gt;_id</span></tt>: the <tt class="docutils literal"><span class="pre">id</span></tt> of the instance which points at the
model (i.e. the source instance).</li>
<li><tt class="docutils literal"><span class="pre">to_&lt;model&gt;_id</span></tt>: the <tt class="docutils literal"><span class="pre">id</span></tt> of the instance to which the relationship
points (i.e. the target model instance).</li>
</ul>
<p>This class can be used to query associated records for a given model
instance like a normal model.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.through_fields">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">through_fields</tt><a class="headerlink" href="#django.db.models.ManyToManyField.through_fields" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Only used when a custom intermediary model is specified. Django will
normally determine which fields of the intermediary model to use in order
to establish a many-to-many relationship automatically. However,
consider the following models:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Group</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>
    <span class="n">members</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">through</span><span class="o">=</span><span class="s">&#39;Membership&#39;</span><span class="p">,</span> <span class="n">through_fields</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;group&#39;</span><span class="p">,</span> <span class="s">&#39;person&#39;</span><span class="p">))</span>

<span class="k">class</span> <span class="nc">Membership</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">group</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Group</span><span class="p">)</span>
    <span class="n">person</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">)</span>
    <span class="n">inviter</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&quot;membership_invites&quot;</span><span class="p">)</span>
    <span class="n">invite_reason</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">64</span><span class="p">)</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">Membership</span></tt> has <em>two</em> foreign keys to <tt class="docutils literal"><span class="pre">Person</span></tt> (<tt class="docutils literal"><span class="pre">person</span></tt> and
<tt class="docutils literal"><span class="pre">inviter</span></tt>), which makes the relationship ambiguous and Django can&#8217;t know
which one to use. In this case, you must explicitly specify which
foreign keys Django should use using <tt class="docutils literal"><span class="pre">through_fields</span></tt>, as in the example
above.</p>
<p><tt class="docutils literal"><span class="pre">through_fields</span></tt> accepts a 2-tuple <tt class="docutils literal"><span class="pre">('field1',</span> <span class="pre">'field2')</span></tt>, where
<tt class="docutils literal"><span class="pre">field1</span></tt> is the name of the foreign key to the model the
<a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> is defined on (<tt class="docutils literal"><span class="pre">group</span></tt> in this case), and
<tt class="docutils literal"><span class="pre">field2</span></tt> the name of the foreign key to the target model (<tt class="docutils literal"><span class="pre">person</span></tt>
in this case).</p>
<p>When you have more than one foreign key on an intermediary model to any
(or even both) of the models participating in a many-to-many relationship,
you <em>must</em> specify <tt class="docutils literal"><span class="pre">through_fields</span></tt>. This also applies to
<a class="reference internal" href="#recursive-relationships"><em>recursive relationships</em></a>
when an intermediary model is used and there are more than two
foreign keys to the model, or you want to explicitly specify which two
Django should use.</p>
<p>Recursive relationships using an intermediary model are always defined as
non-symmetrical &#8211; that is, with <a class="reference internal" href="#django.db.models.ManyToManyField.symmetrical" title="django.db.models.ManyToManyField.symmetrical"><tt class="xref py py-attr docutils literal"><span class="pre">symmetrical=False</span></tt></a>
&#8211; therefore, there is the concept of a &#8220;source&#8221; and a &#8220;target&#8221;. In that
case <tt class="docutils literal"><span class="pre">'field1'</span></tt> will be treated as the &#8220;source&#8221; of the relationship and
<tt class="docutils literal"><span class="pre">'field2'</span></tt> as the &#8220;target&#8221;.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.db_table">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">db_table</tt><a class="headerlink" href="#django.db.models.ManyToManyField.db_table" title="Permalink to this definition">¶</a></dt>
<dd><p>The name of the table to create for storing the many-to-many data. If this
is not provided, Django will assume a default name based upon the names of:
the table for the model defining the relationship and the name of the field
itself.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.db_constraint">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">db_constraint</tt><a class="headerlink" href="#django.db.models.ManyToManyField.db_constraint" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.6.</span> </div>
<p>Controls whether or not constraints should be created in the database for
the foreign keys in the intermediary table. The default is <tt class="docutils literal"><span class="pre">True</span></tt>, and
that&#8217;s almost certainly what you want; setting this to <tt class="docutils literal"><span class="pre">False</span></tt> can be
very bad for data integrity. That said, here are some scenarios where you
might want to do this:</p>
<ul class="simple">
<li>You have legacy data that is not valid.</li>
<li>You&#8217;re sharding your database.</li>
</ul>
<p>It is an error to pass both <tt class="docutils literal"><span class="pre">db_constraint</span></tt> and <tt class="docutils literal"><span class="pre">through</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.swappable">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">swappable</tt><a class="headerlink" href="#django.db.models.ManyToManyField.swappable" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Controls the migration framework&#8217;s reaction if this <a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a>
is pointing at a swappable model. If it is <tt class="docutils literal"><span class="pre">True</span></tt> - the default -
then if the <a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> is pointing at a model which matches
the current value of <tt class="docutils literal"><span class="pre">settings.AUTH_USER_MODEL</span></tt> (or another swappable
model setting) the relationship will be stored in the migration using
a reference to the setting, not to the model directly.</p>
<p>You only want to override this to be <tt class="docutils literal"><span class="pre">False</span></tt> if you are sure your
model should always point towards the swapped-in model - for example,
if it is a profile model designed specifically for your custom user model.</p>
<p>If in doubt, leave it to its default of <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-onetoonefield">
<span id="s-ref-onetoone"></span><span id="onetoonefield"></span><span id="ref-onetoone"></span><h3><tt class="docutils literal"><span class="pre">OneToOneField</span></tt><a class="headerlink" href="#onetoonefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.OneToOneField">
<em class="property">class </em><tt class="descname">OneToOneField</tt>(<em>othermodel</em><span class="optional">[</span>, <em>parent_link=False</em>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.OneToOneField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A one-to-one relationship. Conceptually, this is similar to a
<a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> with <a class="reference internal" href="#django.db.models.Field.unique" title="django.db.models.Field.unique"><tt class="xref py py-attr docutils literal"><span class="pre">unique=True</span></tt></a>, but the
&#8220;reverse&#8221; side of the relation will directly return a single object.</p>
<p>This is most useful as the primary key of a model which &#8220;extends&#8221;
another model in some way; <a class="reference internal" href="../../topics/db/models.html#multi-table-inheritance"><em>Multi-table inheritance</em></a> is
implemented by adding an implicit one-to-one relation from the child
model to the parent model, for example.</p>
<p>One positional argument is required: the class to which the model will be
related. This works exactly the same as it does for <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>,
including all the options regarding <a class="reference internal" href="#recursive-relationships"><em>recursive</em></a>
and <a class="reference internal" href="#lazy-relationships"><em>lazy</em></a> relationships.</p>
<p>If you do not specify the <a class="reference internal" href="#django.db.models.ForeignKey.related_name" title="django.db.models.ForeignKey.related_name"><tt class="xref py py-attr docutils literal"><span class="pre">related_name</span></tt></a> argument for
the <tt class="docutils literal"><span class="pre">OneToOneField</span></tt>, Django will use the lower-case name of the current model
as default value.</p>
<p>With the following example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">MySpecialUser</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">OneToOneField</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">)</span>
    <span class="n">supervisor</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">OneToOneField</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&#39;supervisor_of&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>your resulting <tt class="docutils literal"><span class="pre">User</span></tt> model will have the following attributes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">user</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="s">&#39;myspecialuser&#39;</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="s">&#39;supervisor_of&#39;</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>A <tt class="docutils literal"><span class="pre">DoesNotExist</span></tt> exception is raised when accessing the reverse relationship
if an entry in the related table doesn&#8217;t exist. For example, if a user doesn&#8217;t
have a supervisor designated by <tt class="docutils literal"><span class="pre">MySpecialUser</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">user</span><span class="o">.</span><span class="n">supervisor_of</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="gr">DoesNotExist</span>: <span class="n">User matching query does not exist.</span>
</pre></div>
</div>
<p id="onetoone-arguments">Additionally, <tt class="docutils literal"><span class="pre">OneToOneField</span></tt> accepts all of the extra arguments
accepted by <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>, plus one extra argument:</p>
<dl class="attribute">
<dt id="django.db.models.OneToOneField.parent_link">
<tt class="descclassname">OneToOneField.</tt><tt class="descname">parent_link</tt><a class="headerlink" href="#django.db.models.OneToOneField.parent_link" title="Permalink to this definition">¶</a></dt>
<dd><p>When <tt class="docutils literal"><span class="pre">True</span></tt> and used in a model which inherits from another
(concrete) model, indicates that this field should be used as the
link back to the parent class, rather than the extra
<tt class="docutils literal"><span class="pre">OneToOneField</span></tt> which would normally be implicitly created by
subclassing.</p>
</dd></dl>

<p>See <a class="reference internal" href="../../topics/db/examples/one_to_one.html"><em>One-to-one relationships</em></a> for usage
examples of <tt class="docutils literal"><span class="pre">OneToOneField</span></tt>.</p>
</div>
</div>
<div class="section" id="s-field-api-reference">
<span id="field-api-reference"></span><h2>Field API reference<a class="headerlink" href="#field-api-reference" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.db.models.Field">
<em class="property">class </em><tt class="descname">Field</tt><a class="headerlink" href="#django.db.models.Field" title="Permalink to this definition">¶</a></dt>
<dd><p><tt class="docutils literal"><span class="pre">Field</span></tt> is an abstract class that represents a database table column.
Django uses fields to create the database table (<a class="reference internal" href="#django.db.models.Field.db_type" title="django.db.models.Field.db_type"><tt class="xref py py-meth docutils literal"><span class="pre">db_type()</span></tt></a>), to map
Python types to database (<a class="reference internal" href="#django.db.models.Field.get_prep_value" title="django.db.models.Field.get_prep_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_prep_value()</span></tt></a>) and vice-versa
(<a class="reference internal" href="#django.db.models.Field.to_python" title="django.db.models.Field.to_python"><tt class="xref py py-meth docutils literal"><span class="pre">to_python()</span></tt></a>), and to apply <a class="reference internal" href="lookups.html"><em>Lookup API reference</em></a>
(<a class="reference internal" href="#django.db.models.Field.get_prep_lookup" title="django.db.models.Field.get_prep_lookup"><tt class="xref py py-meth docutils literal"><span class="pre">get_prep_lookup()</span></tt></a>).</p>
<p>A field is thus a fundamental piece in different Django APIs, notably,
<a class="reference internal" href="instances.html#django.db.models.Model" title="django.db.models.Model"><tt class="xref py py-class docutils literal"><span class="pre">models</span></tt></a> and <a class="reference internal" href="querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><tt class="xref py py-class docutils literal"><span class="pre">querysets</span></tt></a>.</p>
<p>In models, a field is instantiated as a class attribute and represents a
particular table column, see <a class="reference internal" href="../../topics/db/models.html"><em>Models</em></a>. It has attributes
such as <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a> and <a class="reference internal" href="#django.db.models.Field.unique" title="django.db.models.Field.unique"><tt class="xref py py-attr docutils literal"><span class="pre">unique</span></tt></a>, and methods that Django uses to
map the field value to database-specific values.</p>
<p>A <tt class="docutils literal"><span class="pre">Field</span></tt> is a subclass of
<a class="reference internal" href="lookups.html#django.db.models.lookups.RegisterLookupMixin" title="django.db.models.lookups.RegisterLookupMixin"><tt class="xref py py-class docutils literal"><span class="pre">RegisterLookupMixin</span></tt></a> and thus both
<a class="reference internal" href="lookups.html#django.db.models.Transform" title="django.db.models.Transform"><tt class="xref py py-class docutils literal"><span class="pre">Transform</span></tt></a> and
<a class="reference internal" href="lookups.html#django.db.models.Lookup" title="django.db.models.Lookup"><tt class="xref py py-class docutils literal"><span class="pre">Lookup</span></tt></a> can be registered on it to be used
in <tt class="docutils literal"><span class="pre">QuerySet</span></tt>s (e.g. <tt class="docutils literal"><span class="pre">field_name__exact=&quot;foo&quot;</span></tt>). All <a class="reference internal" href="querysets.html#field-lookups"><em>built-in
lookups</em></a> are registered by default.</p>
<p>All of Django&#8217;s built-in fields, such as <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a>, are particular
implementations of <tt class="docutils literal"><span class="pre">Field</span></tt>. If you need a custom field, you can either
subclass any of the built-in fields or write a <tt class="docutils literal"><span class="pre">Field</span></tt> from scratch. In
either case, see <a class="reference internal" href="../../howto/custom-model-fields.html"><em>Writing custom model fields</em></a>.</p>
<dl class="attribute">
<dt id="django.db.models.Field.description">
<tt class="descname">description</tt><a class="headerlink" href="#django.db.models.Field.description" title="Permalink to this definition">¶</a></dt>
<dd><p>A verbose description of the field, e.g. for the
<a class="reference internal" href="../contrib/admin/admindocs.html#module-django.contrib.admindocs" title="django.contrib.admindocs: Django's admin documentation generator."><tt class="xref py py-mod docutils literal"><span class="pre">django.contrib.admindocs</span></tt></a> application.</p>
<p>The description can be of the form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">description</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&quot;String (up to </span><span class="si">%(max_length)s</span><span class="s">)&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>where the arguments are interpolated from the field&#8217;s <tt class="docutils literal"><span class="pre">__dict__</span></tt>.</p>
</dd></dl>

<p>To map a <tt class="docutils literal"><span class="pre">Field</span></tt> to a database-specific type, Django exposes two methods:</p>
<dl class="method">
<dt id="django.db.models.Field.get_internal_type">
<tt class="descname">get_internal_type</tt>()<a class="headerlink" href="#django.db.models.Field.get_internal_type" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a string naming this field for backend specific purposes.
By default, it returns the class name.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#emulating-built-in-field-types"><em>Emulating built-in field types</em></a> for usage in custom fields.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.db_type">
<tt class="descname">db_type</tt>(<em>connection</em>)<a class="headerlink" href="#django.db.models.Field.db_type" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the database column data type for the <a class="reference internal" href="#django.db.models.Field" title="django.db.models.Field"><tt class="xref py py-class docutils literal"><span class="pre">Field</span></tt></a>, taking
into account the <tt class="docutils literal"><span class="pre">connection</span></tt>.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#custom-database-types"><em>Custom database types</em></a> for usage in custom fields.</p>
</dd></dl>

<p>There are three main situations where Django needs to interact with the
database backend and fields:</p>
<ul class="simple">
<li>when it queries the database (Python value -&gt; database backend value)</li>
<li>when it loads data from the database (database backend value -&gt; Python
value)</li>
<li>when it saves to the database (Python value -&gt; database backend value)</li>
</ul>
<p>When querying, <a class="reference internal" href="#django.db.models.Field.get_db_prep_value" title="django.db.models.Field.get_db_prep_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_db_prep_value()</span></tt></a> and <a class="reference internal" href="#django.db.models.Field.get_prep_value" title="django.db.models.Field.get_prep_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_prep_value()</span></tt></a> are used:</p>
<dl class="method">
<dt id="django.db.models.Field.get_prep_value">
<tt class="descname">get_prep_value</tt>(<em>value</em>)<a class="headerlink" href="#django.db.models.Field.get_prep_value" title="Permalink to this definition">¶</a></dt>
<dd><p><tt class="docutils literal"><span class="pre">value</span></tt> is the current value of the model&#8217;s attribute, and the method
should return data in a format that has been prepared for use as a
parameter in a query.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#converting-python-objects-to-query-values"><em>Converting Python objects to query values</em></a> for usage.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.get_db_prep_value">
<tt class="descname">get_db_prep_value</tt>(<em>value</em>, <em>connection</em>, <em>prepared=False</em>)<a class="headerlink" href="#django.db.models.Field.get_db_prep_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts <tt class="docutils literal"><span class="pre">value</span></tt> to a backend-specific value. By default it returns
<tt class="docutils literal"><span class="pre">value</span></tt> if <tt class="docutils literal"><span class="pre">prepared=True</span></tt> and <a class="reference internal" href="#django.db.models.Field.get_prep_value" title="django.db.models.Field.get_prep_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_prep_value()</span></tt></a> if is
<tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#converting-query-values-to-database-values"><em>Converting query values to database values</em></a> for usage.</p>
</dd></dl>

<p>When loading data, <a class="reference internal" href="#django.db.models.Field.to_python" title="django.db.models.Field.to_python"><tt class="xref py py-meth docutils literal"><span class="pre">to_python()</span></tt></a> is used:</p>
<dl class="method">
<dt id="django.db.models.Field.to_python">
<tt class="descname">to_python</tt>(<em>value</em>)<a class="headerlink" href="#django.db.models.Field.to_python" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts a value as returned by the database (or a serializer) to a
Python object. It is the reverse of <a class="reference internal" href="#django.db.models.Field.get_prep_value" title="django.db.models.Field.get_prep_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_prep_value()</span></tt></a>.</p>
<p>The default implementation returns <tt class="docutils literal"><span class="pre">value</span></tt>, which is the common case
when the database backend already returns the correct Python type.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#converting-database-values-to-python-objects"><em>Converting database values to Python objects</em></a> for usage.</p>
</dd></dl>

<p>When saving, <a class="reference internal" href="#django.db.models.Field.pre_save" title="django.db.models.Field.pre_save"><tt class="xref py py-meth docutils literal"><span class="pre">pre_save()</span></tt></a> and <a class="reference internal" href="#django.db.models.Field.get_db_prep_save" title="django.db.models.Field.get_db_prep_save"><tt class="xref py py-meth docutils literal"><span class="pre">get_db_prep_save()</span></tt></a> are used:</p>
<dl class="method">
<dt id="django.db.models.Field.get_db_prep_save">
<tt class="descname">get_db_prep_save</tt>(<em>value</em>, <em>connection</em>)<a class="headerlink" href="#django.db.models.Field.get_db_prep_save" title="Permalink to this definition">¶</a></dt>
<dd><p>Same as the <a class="reference internal" href="#django.db.models.Field.get_db_prep_value" title="django.db.models.Field.get_db_prep_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_db_prep_value()</span></tt></a>, but called when the field value
must be <em>saved</em> to the database. By default returns
<a class="reference internal" href="#django.db.models.Field.get_db_prep_value" title="django.db.models.Field.get_db_prep_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_db_prep_value()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.pre_save">
<tt class="descname">pre_save</tt>(<em>model_instance</em>, <em>add</em>)<a class="headerlink" href="#django.db.models.Field.pre_save" title="Permalink to this definition">¶</a></dt>
<dd><p>Method called prior to <a class="reference internal" href="#django.db.models.Field.get_db_prep_save" title="django.db.models.Field.get_db_prep_save"><tt class="xref py py-meth docutils literal"><span class="pre">get_db_prep_save()</span></tt></a> to prepare the value
before being saved (e.g. for <a class="reference internal" href="#django.db.models.DateField.auto_now" title="django.db.models.DateField.auto_now"><tt class="xref py py-attr docutils literal"><span class="pre">DateField.auto_now</span></tt></a>).</p>
<p><tt class="docutils literal"><span class="pre">model_instance</span></tt> is the instance this field belongs to and <tt class="docutils literal"><span class="pre">add</span></tt>
is whether the instance is being saved to the database for the first
time.</p>
<p>It should return the value of the appropriate attribute from
<tt class="docutils literal"><span class="pre">model_instance</span></tt> for this field. The attribute name is in
<tt class="docutils literal"><span class="pre">self.attname</span></tt> (this is set up by <a class="reference internal" href="#django.db.models.Field" title="django.db.models.Field"><tt class="xref py py-class docutils literal"><span class="pre">Field</span></tt></a>).</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#preprocessing-values-before-saving"><em>Preprocessing values before saving</em></a> for usage.</p>
</dd></dl>

<p>Besides saving to the database, the field also needs to know how to
serialize its value (inverse of <a class="reference internal" href="#django.db.models.Field.to_python" title="django.db.models.Field.to_python"><tt class="xref py py-meth docutils literal"><span class="pre">to_python()</span></tt></a>):</p>
<dl class="method">
<dt id="django.db.models.Field.value_to_string">
<tt class="descname">value_to_string</tt>(<em>obj</em>)<a class="headerlink" href="#django.db.models.Field.value_to_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts <tt class="docutils literal"><span class="pre">obj</span></tt> to a string. Used to serialize the value of the field.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#converting-model-field-to-serialization"><em>Converting field data for serialization</em></a> for usage.</p>
</dd></dl>

<p>When a lookup is used on a field, the value may need to be &#8220;prepared&#8221;.
Django exposes two methods for this:</p>
<dl class="method">
<dt id="django.db.models.Field.get_prep_lookup">
<tt class="descname">get_prep_lookup</tt>(<em>lookup_type</em>, <em>value</em>)<a class="headerlink" href="#django.db.models.Field.get_prep_lookup" title="Permalink to this definition">¶</a></dt>
<dd><p>Prepares <tt class="docutils literal"><span class="pre">value</span></tt> to the database prior to be used in a lookup.
The <tt class="docutils literal"><span class="pre">lookup_type</span></tt> will be one of the valid Django filter lookups:
<tt class="docutils literal"><span class="pre">&quot;exact&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;iexact&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;contains&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;icontains&quot;</span></tt>,
<tt class="docutils literal"><span class="pre">&quot;gt&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;gte&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;lt&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;lte&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;in&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;startswith&quot;</span></tt>,
<tt class="docutils literal"><span class="pre">&quot;istartswith&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;endswith&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;iendswith&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;range&quot;</span></tt>,
<tt class="docutils literal"><span class="pre">&quot;year&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;month&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;day&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;isnull&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;search&quot;</span></tt>,
<tt class="docutils literal"><span class="pre">&quot;regex&quot;</span></tt>, and <tt class="docutils literal"><span class="pre">&quot;iregex&quot;</span></tt>.</p>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>If you are using <a class="reference internal" href="lookups.html"><em>Custom lookups</em></a> the
<tt class="docutils literal"><span class="pre">lookup_type</span></tt> can be any <tt class="docutils literal"><span class="pre">lookup_name</span></tt> registered in the field.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#preparing-values-for-use-in-database-lookups"><em>Preparing values for use in database lookups</em></a> for usage.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.get_db_prep_lookup">
<tt class="descname">get_db_prep_lookup</tt>(<em>lookup_type</em>, <em>value</em>, <em>connection</em>, <em>prepared=False</em>)<a class="headerlink" href="#django.db.models.Field.get_db_prep_lookup" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to <a class="reference internal" href="#django.db.models.Field.get_db_prep_value" title="django.db.models.Field.get_db_prep_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_db_prep_value()</span></tt></a>, but for performing a lookup.</p>
<p>As with <a class="reference internal" href="#django.db.models.Field.get_db_prep_value" title="django.db.models.Field.get_db_prep_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_db_prep_value()</span></tt></a>, the specific connection that will
be used for the query is passed as <tt class="docutils literal"><span class="pre">connection</span></tt>. In addition,
<tt class="docutils literal"><span class="pre">prepared</span></tt> describes whether the value has already been prepared with
<a class="reference internal" href="#django.db.models.Field.get_prep_lookup" title="django.db.models.Field.get_prep_lookup"><tt class="xref py py-meth docutils literal"><span class="pre">get_prep_lookup()</span></tt></a>.</p>
</dd></dl>

<p>When using <a class="reference internal" href="../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><tt class="xref py py-class docutils literal"><span class="pre">model</span> <span class="pre">forms</span></tt></a>, the <tt class="docutils literal"><span class="pre">Field</span></tt>
needs to know which form field it should be represented by:</p>
<dl class="method">
<dt id="django.db.models.Field.formfield">
<tt class="descname">formfield</tt>(<em>form_class=None</em>, <em>choices_form_class=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.db.models.Field.formfield" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the default <a class="reference internal" href="../forms/fields.html#django.forms.Field" title="django.forms.Field"><tt class="xref py py-class docutils literal"><span class="pre">django.forms.Field</span></tt></a> of this field for
<a class="reference internal" href="../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><tt class="xref py py-class docutils literal"><span class="pre">ModelForm</span></tt></a>.</p>
<p>By default, if both <tt class="docutils literal"><span class="pre">form_class</span></tt> and <tt class="docutils literal"><span class="pre">choices_form_class</span></tt> are
<tt class="docutils literal"><span class="pre">None</span></tt>, it uses <a class="reference internal" href="../forms/fields.html#django.forms.CharField" title="django.forms.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a>; if
<tt class="docutils literal"><span class="pre">choices_form_class</span></tt> is given, it returns
<a class="reference internal" href="../forms/fields.html#django.forms.TypedChoiceField" title="django.forms.TypedChoiceField"><tt class="xref py py-class docutils literal"><span class="pre">TypedChoiceField</span></tt></a>.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#specifying-form-field-for-model-field"><em>Specifying the form field for a model field</em></a> for usage.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.deconstruct">
<tt class="descname">deconstruct</tt>()<a class="headerlink" href="#django.db.models.Field.deconstruct" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Returns a 4-tuple with enough information to recreate the field:</p>
<ol class="arabic simple">
<li>The name of the field on the model.</li>
<li>The import path of the field (e.g. <tt class="docutils literal"><span class="pre">&quot;django.db.models.IntegerField&quot;</span></tt>).
This should be the most portable version, so less specific may be better.</li>
<li>A list of positional arguments.</li>
<li>A dict of keyword arguments.</li>
</ol>
<p>This method must be added to fields prior to 1.7 to migrate its data
using <a class="reference internal" href="../../topics/migrations.html"><em>Migrations</em></a>.</p>
</dd></dl>

</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Model field reference</a><ul>
<li><a class="reference internal" href="#field-options">Field options</a><ul>
<li><a class="reference internal" href="#null"><tt class="docutils literal"><span class="pre">null</span></tt></a></li>
<li><a class="reference internal" href="#blank"><tt class="docutils literal"><span class="pre">blank</span></tt></a></li>
<li><a class="reference internal" href="#choices"><tt class="docutils literal"><span class="pre">choices</span></tt></a></li>
<li><a class="reference internal" href="#db-column"><tt class="docutils literal"><span class="pre">db_column</span></tt></a></li>
<li><a class="reference internal" href="#db-index"><tt class="docutils literal"><span class="pre">db_index</span></tt></a></li>
<li><a class="reference internal" href="#db-tablespace"><tt class="docutils literal"><span class="pre">db_tablespace</span></tt></a></li>
<li><a class="reference internal" href="#default"><tt class="docutils literal"><span class="pre">default</span></tt></a></li>
<li><a class="reference internal" href="#editable"><tt class="docutils literal"><span class="pre">editable</span></tt></a></li>
<li><a class="reference internal" href="#error-messages"><tt class="docutils literal"><span class="pre">error_messages</span></tt></a></li>
<li><a class="reference internal" href="#help-text"><tt class="docutils literal"><span class="pre">help_text</span></tt></a></li>
<li><a class="reference internal" href="#primary-key"><tt class="docutils literal"><span class="pre">primary_key</span></tt></a></li>
<li><a class="reference internal" href="#unique"><tt class="docutils literal"><span class="pre">unique</span></tt></a></li>
<li><a class="reference internal" href="#unique-for-date"><tt class="docutils literal"><span class="pre">unique_for_date</span></tt></a></li>
<li><a class="reference internal" href="#unique-for-month"><tt class="docutils literal"><span class="pre">unique_for_month</span></tt></a></li>
<li><a class="reference internal" href="#unique-for-year"><tt class="docutils literal"><span class="pre">unique_for_year</span></tt></a></li>
<li><a class="reference internal" href="#verbose-name"><tt class="docutils literal"><span class="pre">verbose_name</span></tt></a></li>
<li><a class="reference internal" href="#validators"><tt class="docutils literal"><span class="pre">validators</span></tt></a><ul>
<li><a class="reference internal" href="#registering-and-fetching-lookups">Registering and fetching lookups</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#field-types">Field types</a><ul>
<li><a class="reference internal" href="#autofield"><tt class="docutils literal"><span class="pre">AutoField</span></tt></a></li>
<li><a class="reference internal" href="#bigintegerfield"><tt class="docutils literal"><span class="pre">BigIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#binaryfield"><tt class="docutils literal"><span class="pre">BinaryField</span></tt></a></li>
<li><a class="reference internal" href="#booleanfield"><tt class="docutils literal"><span class="pre">BooleanField</span></tt></a></li>
<li><a class="reference internal" href="#charfield"><tt class="docutils literal"><span class="pre">CharField</span></tt></a></li>
<li><a class="reference internal" href="#commaseparatedintegerfield"><tt class="docutils literal"><span class="pre">CommaSeparatedIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#datefield"><tt class="docutils literal"><span class="pre">DateField</span></tt></a></li>
<li><a class="reference internal" href="#datetimefield"><tt class="docutils literal"><span class="pre">DateTimeField</span></tt></a></li>
<li><a class="reference internal" href="#decimalfield"><tt class="docutils literal"><span class="pre">DecimalField</span></tt></a></li>
<li><a class="reference internal" href="#emailfield"><tt class="docutils literal"><span class="pre">EmailField</span></tt></a></li>
<li><a class="reference internal" href="#filefield"><tt class="docutils literal"><span class="pre">FileField</span></tt></a><ul>
<li><a class="reference internal" href="#filefield-and-fieldfile">FileField and FieldFile</a></li>
</ul>
</li>
<li><a class="reference internal" href="#filepathfield"><tt class="docutils literal"><span class="pre">FilePathField</span></tt></a></li>
<li><a class="reference internal" href="#floatfield"><tt class="docutils literal"><span class="pre">FloatField</span></tt></a></li>
<li><a class="reference internal" href="#imagefield"><tt class="docutils literal"><span class="pre">ImageField</span></tt></a></li>
<li><a class="reference internal" href="#integerfield"><tt class="docutils literal"><span class="pre">IntegerField</span></tt></a></li>
<li><a class="reference internal" href="#ipaddressfield"><tt class="docutils literal"><span class="pre">IPAddressField</span></tt></a></li>
<li><a class="reference internal" href="#genericipaddressfield"><tt class="docutils literal"><span class="pre">GenericIPAddressField</span></tt></a></li>
<li><a class="reference internal" href="#nullbooleanfield"><tt class="docutils literal"><span class="pre">NullBooleanField</span></tt></a></li>
<li><a class="reference internal" href="#positiveintegerfield"><tt class="docutils literal"><span class="pre">PositiveIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#positivesmallintegerfield"><tt class="docutils literal"><span class="pre">PositiveSmallIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#slugfield"><tt class="docutils literal"><span class="pre">SlugField</span></tt></a></li>
<li><a class="reference internal" href="#smallintegerfield"><tt class="docutils literal"><span class="pre">SmallIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#textfield"><tt class="docutils literal"><span class="pre">TextField</span></tt></a></li>
<li><a class="reference internal" href="#timefield"><tt class="docutils literal"><span class="pre">TimeField</span></tt></a></li>
<li><a class="reference internal" href="#urlfield"><tt class="docutils literal"><span class="pre">URLField</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-django.db.models.fields.related">Relationship fields</a><ul>
<li><a class="reference internal" href="#foreignkey"><tt class="docutils literal"><span class="pre">ForeignKey</span></tt></a><ul>
<li><a class="reference internal" href="#database-representation">Database Representation</a></li>
<li><a class="reference internal" href="#arguments">Arguments</a></li>
</ul>
</li>
<li><a class="reference internal" href="#manytomanyfield"><tt class="docutils literal"><span class="pre">ManyToManyField</span></tt></a><ul>
<li><a class="reference internal" href="#id1">Database Representation</a></li>
<li><a class="reference internal" href="#manytomany-arguments">Arguments</a></li>
</ul>
</li>
<li><a class="reference internal" href="#onetoonefield"><tt class="docutils literal"><span class="pre">OneToOneField</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#field-api-reference">Field API reference</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="index.html">Models</a></li>
    
    
      <li>Next: <a href="relations.html">Related objects reference</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">API Reference</a>
        
          <ul><li><a href="index.html">Models</a>
        
        <ul><li>Model field reference</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/ref/models/fields.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="Models">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="relations.html" title="Related objects reference">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>